1

I have an array like this :

 Array<int> arr = [1,3,98,12,...]

and I want to get all documents having Id which belongs to that array

 db.collection.find({"key": arr})

It would be crazy to use

  for (int i=1; i<= arr.length(); i++)
  {
    db.collection.find({"key": i})
  }

Instead, please help me if you know an effective way. Thanks !

1

2 Answers 2

2

Use the $in operator.

db.collection.find({"key" : {"$in" : arr} })

The above query returns any document if its key is in arr.

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

Comments

2

Try the $in operator as described in the documentation. Something like this should do the trick:

db.collection.find({ "key": { "$in" : arr} })

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.