Articles → MONGODB → $Lookup Operator In Mongodb
$Lookup Operator In Mongodb
Purpose
Syntax
db.collection.aggregate([
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
])
- The from specifies the collection to join with
- The localField specifies the field from the input document to match the foreign key in the from collection
- The foreignField specifies the field of the from collection to match with the localField
- The as field specifies the name the output array
Collections
Click to Enlarge
Click to Enlarge
$Lookup Operator
db.Articles.aggregate([ { $lookup: { from: "ArticlesComments", localField: "_id", foreignField: "_subjectid", as: "ArticleDetails" } }])
Output
Click to Enlarge