I want to make an efficient query in MongoDb to find all users who have their userids listed in a usergroup. Ideally I want to make this as a single request to Mongodb. What I w
db.usergroup.aggregate([
{ $match: { _id: "g1" } },
{ $unwind: "$Users" },
{ $lookup:
{ from: "user", localField: "Users", foreignField: "_id", as: "user" }
}
])
// result :
{ "_id" : "g1", "Users" : "u2", "user" : [ { "_id" : "u2", "Name" : "u1 name" } ] }
{ "_id" : "g1", "Users" : "u3", "user" : [ { "_id" : "u3", "Name" : "u3 name" } ] }