子查询为集合B mongodb中的键替换折叠A中列表中包装的值

子查询为集合B mongodb中的键替换折叠A中列表中包装的值,mongodb,Mongodb,我对mongodb有一个疑问 我的数据库中有两个集合:名称、状态和菜单 status _id中的主键是菜单集合中购买列表值的外键 对于状态收集: { "_id": "green", "description": "running" } { "_id": "yellow", "description": "prepareing" } { "_id": "black", "description": "closing" } { "_id": "red", "descr

我对mongodb有一个疑问

我的数据库中有两个集合:名称、状态和菜单

status _id中的主键是菜单集合中购买列表值的外键

对于状态收集:

{
    "_id": "green", "description": "running"
}
{
    "_id": "yellow", "description": "prepareing"
}
{
    "_id": "black", "description": "closing"
}
{
    "_id": "red", "description": "repairing"
}
对于菜单集合:

{
    "name": "tony",
    "bought": [
        {
            "notebook": "green"
        },
        {
            "cellphone": "red"
        }
    ]
}
{
    "name": "andy",
    "bought": [
        {
            "fan": "black"
        }
    ]
}
如何查询以获得以下答案

只需替换_id的描述


这是NoSQL的子查询问题吗?如何使用google的关键字?

这里有一个使用聚合的版本:

我们从一个阶段开始,提取每一行中购买的内容

然后,a将对所购买的字段进行规范化

然后,我们可以在状态上执行加入

然后我们用名字重新组合

并重新设置为非规范化样式

> db.menu.find()
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268b"), "name" : "tony", "bought" : [ { "notebook" : "green" }, { "cellphone" : "red" } ] }
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268c"), "name" : "andy", "bought" : [ { "fan" : "black" } ] }
> db.status.find()
{ "_id" : "green", "description" : "running" }
{ "_id" : "yellow", "description" : "prepareing" }
{ "_id" : "black", "description" : "closing" }
{ "_id" : "red", "description" : "repairing" }
> db.menu.aggregate([
{$unwind: '$bought'}, 
{$project: {name: 1, bought: {$objectToArray: '$bought'}}}, {$unwind: '$bought'}, 
{$lookup: {from: 'status', localField: 'bought.v', foreignField: '_id', as: "status"}}, 
{$project: {name: 1, bought: ["$bought.k", { $arrayElemAt: ["$status.description", 0]}]}}, 
{$addFields: {b: {v: {$arrayElemAt: ['$bought', 1]}, k: { $arrayElemAt: ['$bought', 0]}}}}, 
{$group: {_id: { name: '$name', _id: "$_id"}, b: {$push: "$b"}}},     
{$project: {_id: "$_id._id", name: "$_id.name", bought: {$arrayToObject: "$b"}}}
])
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268c"), "name" : "andy", "bought" : { "fan" : "closing" } }
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268b"), "name" : "tony", "bought" : { "notebook" : "running", "cellphone" : "repairing" } }

我认为它可以用更简单的方式执行,但我不知道如何执行,我很高兴知道。

谢谢!这对我很有用。我还想知道一个更简单的答案。
> db.menu.find()
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268b"), "name" : "tony", "bought" : [ { "notebook" : "green" }, { "cellphone" : "red" } ] }
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268c"), "name" : "andy", "bought" : [ { "fan" : "black" } ] }
> db.status.find()
{ "_id" : "green", "description" : "running" }
{ "_id" : "yellow", "description" : "prepareing" }
{ "_id" : "black", "description" : "closing" }
{ "_id" : "red", "description" : "repairing" }
> db.menu.aggregate([
{$unwind: '$bought'}, 
{$project: {name: 1, bought: {$objectToArray: '$bought'}}}, {$unwind: '$bought'}, 
{$lookup: {from: 'status', localField: 'bought.v', foreignField: '_id', as: "status"}}, 
{$project: {name: 1, bought: ["$bought.k", { $arrayElemAt: ["$status.description", 0]}]}}, 
{$addFields: {b: {v: {$arrayElemAt: ['$bought', 1]}, k: { $arrayElemAt: ['$bought', 0]}}}}, 
{$group: {_id: { name: '$name', _id: "$_id"}, b: {$push: "$b"}}},     
{$project: {_id: "$_id._id", name: "$_id.name", bought: {$arrayToObject: "$b"}}}
])
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268c"), "name" : "andy", "bought" : { "fan" : "closing" } }
{ "_id" : ObjectId("5a102b0b49b317e3f8d6268b"), "name" : "tony", "bought" : { "notebook" : "running", "cellphone" : "repairing" } }