2

Say I have a Model that looks like this:

{ name: String, category: String }

And I got an Array with objects like this:

var array = [{
    { name: "a", category: "A" }
  , { name: "b, category: "A" }
  , { name: "b", category: "B" }
}]

Now I want to find all objects that matches the value pairs in the array. If it was an array of Strings only, the code would be something like Model.find({ name: {$in: array } }), but I need something like

Model.find({ name,category: {$in: array} })

Is this possible?

1 Answer 1

5

You could directly use that array in an $or query to look for docs that match any of those pairs:

var array = [
    { name: "a", category: "A" }
  , { name: "b", category: "A" }
  , { name: "b", category: "B" }
];

Model.find({$or: array}, function(err, docs) { ... });
Sign up to request clarification or add additional context in comments.

2 Comments

or using mongoose's .or helper - Model.find().or(array)
How did I not think of that? Thank you so much!

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.