I'm trying to get an array of id's from a document in a collection(Users) and then get their names by finding the document with that id in another collection(Benefits).
I've tried reading up on $lookup but the documention is confusing, as it doesn't give a real world example.
This returns the benefit's from the user, which I can grasp.
Users.findOne({username: req.params.username},{benefits: 1, _id: 0 }, function(err, user) {
if(err)
console.log(err);
else
res.json(user);
});
This is a collection example of the user which contains an array of IDs.
{
"_id" : ObjectId("5b997cf8082be41757b6d733"),
"username" : "anonuser",
"benefitsIds" : [
ObjectId("5ba8345f1e56fe8e6caaaa07"),
ObjectId("5ba706d64e82292e72e9ae71")
]
}
Now in the other collection(Benefits) I have the benefits with their names. This is a collection example.
{
"_id" : ObjectId("5ba706d64e82292e72e9ae71"),
"benefit_name" : "Dental"
}
I just want to return the actual benefit name.