lookup in aggregae in mongodb code example

Example 1: lookup in mongodb array

> db.demo396.aggregate([
...    { "$lookup": {
...       "from": "demo395",
...       "let": { "details": "$details" },
...       "pipeline": [
...          { "$match": { "$expr": { "$in": [ "$_id", "$$details" ] } } }
...       ],
...       "as": "output"
...    }}
... ])

Example 2: lookup in mongodb array

{ "_id" : ObjectId("5e5e787817aa3ef9ab8ab209"), "details" : [ ObjectId("5e5e782317aa3ef9ab8ab207"), ObjectId("5e5e782317aa3ef9ab8ab208") ], "output" : [ { "_id" : ObjectId("5e5e782317aa3ef9ab8ab207"), "Name" : "Chris" }, { "_id" : ObjectId("5e5e782317aa3ef9ab8ab208"), "Name" : "David" } ] }

Tags:

Misc Example