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 !

Dennis Do
  • 480
  • 7
  • 24
  • Since you tagged this as Java, http://stackoverflow.com/questions/11650970/mongodb-java-finding-objects-in-mongo-using-querybuilder-in-operator-returns-n seems like a reasonable match using `$in`. – WiredPrairie Dec 29 '13 at 19:52

2 Answers2

2

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

db.collection.find({ "key": { "$in" : arr} })
Matt
  • 17,290
  • 7
  • 57
  • 71
2

Use the $in operator.

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

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

4J41
  • 5,005
  • 1
  • 29
  • 41