Skip to content

Instantly share code, notes, and snippets.

@Sidheeqpallam
Created August 23, 2022 14:25
Show Gist options
  • Save Sidheeqpallam/9028062930c7877d9ce385071220c888 to your computer and use it in GitHub Desktop.
Save Sidheeqpallam/9028062930c7877d9ce385071220c888 to your computer and use it in GitHub Desktop.
aggregation lookup with foreign field _id
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