Request MongoDB for documents whose identifiers are contained in a list

Suppose you have an id-s list that can contain up to a thousand identifiers for documents inside a database. What is the best way to return these documents?

Should I call a query for each of them or should I specify a giant OR query? maybe there is something better i don't know about

+6
source share
2 answers

There is: $ in:

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

In C # $ in code:

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

Source: https://habr.com/ru/post/900018/


All Articles