Texonom
Texonom
/
Engineering
Engineering
/Data Engineering/DataBase/Database Type/NoSQL/Document DB/
mongoDB
mongoDB
/Mongo Pipeline/
mongo $lookup
Search

mongo $lookup

Mongo Join

Example

db.facility.aggregate([ { $lookup: { from: "facilities", localField: "created_by", foreignField: "id", as: "maker" } } ])
 

Findone

[ { $lookup: { from: "users", localField: "edited_by", foreignField: "id", as: "editor" } }, { "$project": { "editor": { "$arrayElemAt": [ "$editor", 0 ] } }} ]
 
 

Also findone

How to use MongoDBs aggregate `$lookup` as `findOne()`
So as you all know, find() returns an array of results, with findOne() returning just a simply object. With Angular, this makes a huge difference. Instead of going {{myresult[0].name}}, I can simply
How to use MongoDBs aggregate `$lookup` as `findOne()`
https://stackoverflow.com/questions/37691727/how-to-use-mongodbs-aggregate-lookup-as-findone
How to use MongoDBs aggregate `$lookup` as `findOne()`
$lookup (aggregation) - MongoDB Manual
Performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing. To each input document, the stage adds a new array field whose elements are the matching documents from the "joined" collection. The stage passes these reshaped documents to the next stage.
$lookup (aggregation) - MongoDB Manual
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
$lookup (aggregation) - MongoDB Manual

Recommendations

Texonom
Texonom
/
Engineering
Engineering
/Data Engineering/DataBase/Database Type/NoSQL/Document DB/
mongoDB
mongoDB
/Mongo Pipeline/
mongo $lookup
Copyright Seonglae Cho