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>
}
}
])
| Parameter | Explanation |
|---|
| from | specifies the collection to join with |
| localField | specifies the field from the input document to match the foreign key in the from collection |
| foreignField | specifies the field of the from collection to match with the localField |
| as | specifies the name the output array |
Collections
$Lookup Operator
db.Articles.aggregate([
{
$lookup: {
from: "ArticlesComments",
localField: "_id",
foreignField: "_subjectid",
as: "ArticleDetails"
}
}
]);
Output
| Posted By - | Karan Gupta |
| |
| Posted On - | Friday, May 5, 2023 |
| |
| Updated On - | Friday, October 13, 2023 |