Yurii Hohan
Yurii Hohan

Reputation: 4171

Query the MongoDB for the documents whose id-s are contained inside a list

Suppose you have a list of id-s which can contain up to thousands id-s for the documents inside the database. What is the best way to get those documents back?

Should I invoke a query for each of them or should I specify a giant OR query? may be there is something way better I do not know of

Upvotes: 6

Views: 3330

Answers (2)

Andrew Orsich
Andrew Orsich

Reputation: 53705

In c# code, $in:

var ids = new int[] {1, 2, 3, 4, 5};
var query = Query.In("name", BsonArray.Create(ids));
var items = collection.Find(query);

Upvotes: 8

Remon van Vliet
Remon van Vliet

Reputation: 18625

There is, $in :

db.co.find({_id:{$in:[id1, id2, id3, .., idN]}})

Upvotes: 7

Related Questions