[英]Find/aggregate from another collection based on its _id in mongodb
[英]Mongodb get element value from another collection based on its _id
我正在嘗試根據聚合中的結果 _id 獲取元素值。
這是聚合:
$project: {
_id: 0,
brand: "$_id",
"options": {
$mergeObjects: "$ram"
},
sum: {
$add: [
"$sm",
1
]
}
}
我希望品牌從其他名為“品牌”的集合中獲取名稱,如下所示
[
{
"_id": ObjectId("617b0dbacda6cbd1a0403f68"),
"SerialNumber": "45454234324",
"name": "hp"
},
{
"_id": ObjectId("617b0dbacda6cbd1a0403f69"),
"SerialNumber": "azazz5245454az",
"name": "asus"
}]
我想要得到的是使用基於結果_id 的_id 的品牌名稱。 使用 SQL 它是這樣的:
Get brands.name where _id=brands._id
我設法使用 $lookup 做到了
{
$lookup:
{
from: "brands",
localField: "brand",
foreignField: "_id",
as: "brand"
}
},
{
$set: {
brand: "$brand.brand"
}
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.