Created
August 23, 2022 14:25
-
-
Save Sidheeqpallam/9028062930c7877d9ce385071220c888 to your computer and use it in GitHub Desktop.
aggregation lookup with foreign field _id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollection('tests').aggregate([ | |
{$match: {typet:'Req'}}, | |
{$set: {incharge: {$toObjectId: "$incharge"} }}, // keep the whole document structure, but replace `incharge` into ObjectId | |
{$lookup:{ | |
from: "users", | |
localField: "incharge", //this is the _id user from tests | |
foreignField: "_id", //this is the _id from users | |
as: "user" | |
}} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment