1

Is there a way to search a mongodb database using an array of objects as search arguments?

Lets say I have the following search preferences:

preferences = [{
    product: "PRODUCT_A",
    minqty: 5,
    maxqty: 50
  },
  {
    product: "PRODUCT_B",
    minqty: 100,
    maxqty: 500
  }
]

In my database I have Jobs with the following structure:

{
  jobName: "job name",
  items: [{
      product: "PRODUCT_A"
      qty: 25
    },
    {
      product: "PRODUCT_F"
      qty: 300
    }

  ]
}

I would like to query the database using preferences and returning any jobs that match at least one of the criteria's.

ATTEMPT 1:

I managed to use all my preferences as filters, but $match is cumulative, the way it's written it works like && in javascript. My goal is to have "match THIS || match THAT".

let pipeline = [];

preferences.map((item) => {
  let search = {
    product: item.product,
    quantity: { $gte: item.minqty, $lte: item.maxqty },
  };

  return pipeline.push({
    $match: {
      items: {
        $elemMatch: search,
      },
    },
  });
});

const jobs = await Job.aggregate(pipeline);

ATTEMPS 2 (SUCCESS):

let search = [];

preferences.map((item, index) => {
  let arguments = {
    product: item.product,
    quantity: { $gte: item.minqty, $lte: item.maxqty },
  };

  search.push(arguments);
});

let pipeline = [
  {
    $match: {
      items: {
        $elemMatch: {
          $or: search,
        },
      },
    },
  },
];

const jobs = await Job.aggregate(pipeline);

1 Answer 1

1

Use aggregation

  1. Denormalize items using $Unwind
  2. Once denormalized, you can use simple match with $or
  3. Use $lte and $gte

And update the question with your attempts of these or post a new one.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the insight, I'll try and update the question with a resolution guide if possible
solved the problem, thanks for helping, I had never used aggregation before, it's really powerful tool
idk if it's appropriate to mark as the solution when $unwind was not necessary
ElemMatch ll return the first matching element un the array. Not all matching elements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.