我有两个名为“orders”和“items”的集合,如下所示:
db.orders.insert([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }
])
db.items.insert([
{ "_id" : 1, "item" : "almonds", description: "almond clusters", "instock" : 120 },
{ "_id" : 2, "item" : "bread", description: "raisin and nut bread", "instock" : 80 },
{ "_id" : 3, "item" : "pecans", description: "candied pecans", "instock" : 60 }
])
现在,如果两个集合中的“item”字段匹配,我正在尝试获取文档。
我尝试在这种情况下使用$ lookup运算符,但尝试在mongo shell上运行时,查询失败了。
db.orders.aggregate([
{
$lookup:
{
from: "items",
localField: “item”,
foreignField: “item”,
as: "ite"
}
},
{ $unwind: "$ite" },
{
$project: {
“item”: 1
}
}
])
任何人都可以帮我解决上述情况...