mongodb - perform batch query

后端 未结 2 849
孤街浪徒
孤街浪徒 2021-02-13 20:46

I need query data from collection a first, then according to those data, query from collection b. Such as:

For each id queried from a
    query data from b where         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 21:11

    It's very simple, don't make so many DB calls for each id, it is very inefficient, it is possible to execute a single query which will return all documents relevant to each of the ids in a single pass using the $in operator in MongoDB, which is synonymous to in syntax in SQL so for example if you need to find out the documents for 5 ids in a single pass then

    const ids = ['id1', 'id2', 'id3', 'id4', 'id5'];
    const results = db.collectionName.find({ _id : { $in : ids }})
    

    This will get you all the relevant documents in a single pass.

提交回复
热议问题