2

I have an string array of id's that i'd like to use with the find function.

db.companies.find( { _id : { $in : arr} });

arr looks something like this,

[ '563a2c60b511b7ff2c61e938', '563a2c60b511b7ff2c61e8b7' ];

I see from the documentation that ObjectID() doesn't accept arrays. How can I search for a list of documents with this array? Will I have to recreate the array so that all elements are ObjectID's?

1
  • What is the output when you run that query? Commented Dec 15, 2015 at 22:31

1 Answer 1

7

One option is to use the map function to get the list of ObjectId's from the list of string ids:

arr.map(function (id) {
  return ObjectId(id);
})

Plugged into your query:

db.companies.find({_id: { $in: arr.map(function (id) {return ObjectId(id);})}})
Sign up to request clarification or add additional context in comments.

Comments

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.