Essentially, there are 2 queries. The first one queries a collection_A and then return an array of _id
. The second query will use this array in a $in
operator to query collection_B.
How can this be done in just 1 query instead of 2? I.e., how can this be done with one call to MongoDB server instead of 2 trips?
2
Answers
I think what you need is $lookup, since oy connect documents by _id between collection_A and collection_B.
mongoplayground
The $lookup will join two collections either on a simple localField foreignField match or with a more complicated syntax let/pipeline, see
The simpler format, with a match, your first query, is as follows: