I have two collections and I want to join those two collections this is my first collection:
{
"taskId": "62925ec4e9ebd8e244287299",
"gebruikerId": "6287cd19af1540ea55212fee",
"fotoUrl": "<url>",
"afgerond": false,
"notitie": "",
"_id": "62927baabc3b9a3603ad56aa",
"createdAt": "2022-05-28T19:44:42.559Z",
"updatedAt": "2022-05-28T19:44:42.559Z",
"__v": 0
}
And I want to join the taskId of the previous collection on the _id of this collection:
{
"_id": "62925ec4e9ebd8e244287299",
"taskNaam": "BodyShot",
"omschrijving": "Neem een bodyshot uit de navel van Nils",
"punten": 100,
"fotoUrl": "<url>",
"locked": true,
"createdAt": "2022-05-28T17:41:24.358Z",
"updatedAt": "2022-05-28T17:41:24.358Z",
"__v": 0
}
Simply use $lookup in an aggregation stage like this:
db.collection1.aggregate([
{
"$lookup": {
"from": "collection2",
"localField": "taskId",
"foreignField": "_id",
"as": "tasks"
}
}
])
This query compare the fields taskId
in collection1 and _id
in collection2, showing the join in a field called tasks
.
Example here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.