0

So I have this array with 3 IDs: ["56ba576f66b9add00e4d3082","56b9252266b9add00e4d3080","56b908f166b9add00e4d307f"]

I want to build a query that says something like that:

userModel.find({ $or: 
  [ 
    {_id:"56ba576f66b9add00e4d3082"}, 
    {_id:"56ba576f66b9add00e4d3082"}, 
    {_id:"56ba576f66b9add00e4d3082"} 
  ]}, 
    {email:true});

Is there some sort of a method to use in order to produce such query with the array? Should I be using mapReduce or the $where method or anything else? (I never used those before so I'm not sure if this is the case or not)

Perhaps I'm asking the wrong question? Any recommendations are very much welcome.

2 Answers 2

2

If you just have one element then just use $in:

var array = [
    "56ba576f66b9add00e4d3082",
    "56b9252266b9add00e4d3080",
    "56b908f166b9add00e4d307f"
];

userModel.find({ "_id": { "$in": array } }, { "email": true });

An $in condition is basically an $or applied to the same field.

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

Comments

0

Try using the map function:

array = ["56ba576f66b9add00e4d3082","56b9252266b9add00e4d3080","56b908f166b9add00e4d307f"]

userModel.find({ $or: array.map(function(id) {
    return {_id: id};
})}, {email:true});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.