作为子文档获取一个集合的数据在另一个文档中mongoDb
问题描述:
我正在使用猫鼬。我有一个meta
收集这样作为子文档获取一个集合的数据在另一个文档中mongoDb
{
"_id" : ObjectId("597568f5cf12434674"),
"data": "test data",
"createBy" : "testing",
"modifyBy" : "testing",
"modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
"createDate" : ISODate("2017-07-24T03:26:45.350Z"),
}
这_id
是叫orders
作为metaId
{
"_id" : ObjectId("597818bf5332f91213411"),
"type" : "food order",
"isActive" : true,
"files" : [
{
"metaId" : "597568f5cf12434674",
"_id" : ObjectId("597818sdfasdaf2222")
}
]
}
,当我拿到订单的细节我需要加入两个集合,所以我的最终结果将是另一个集合像这样
{
"_id" : ObjectId("597818bf5332f91213411"),
"type" : "food order",
"isActive" : true,
"files" : [
{
"metaId" : "597568f5cf12434674",
"data": "test data",
"createBy" : "testing",
"modifyBy" : "testing",
"modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
"createDate" : ISODate("2017-07-24T03:26:45.350Z"),
"_id" : ObjectId("597818sdfasdaf2222")
}
]
}
我写了这个查询但返回空,我知道这将是inc直接,因为我希望元数据在订单中。
const query = db.orders.aggregate([
{'$match': {'_id': _id}},
{'$lookup': {
'localField': 'files.metaId',
'from': 'meta',
'foreignField': '_id',
'as': 'metaInfo'
}},
{'$unwind': '$metaInfo'},
{'$project': {
'type': 1,
'isActive': 1,
'metaInfo.data': 1
}}
]);
有反正我可以做这个mongo,我对mongodb很新。请提前帮助,谢谢。
答
上$lookup
汇聚运营商不会与一个数组的工作“从”边,所以你必须先解开它:
db.getCollection('orders').aggregate([
// matches a single document in the orders collection
{'$match': {'_id': ObjectId("59784eb56149554af36a5666")}},
// unwinds orders.files so that it is no longer an array
{'$unwind': '$files'},
// performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files"
{'$lookup': {
'localField': 'files.metaId',
'from': 'meta',
'foreignField': '_id',
'as': 'files'
}}
]);
上面的命令将返回以下文件:
{
"_id" : ObjectId("59784eb56149554af36a5666"),
"type" : "food order",
"isActive" : true,
"files" : [
{
"_id" : ObjectId("59784e706149554af36a5621"),
"data" : "test data",
"createBy" : "testing",
"modifyBy" : "testing",
"modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
"createDate" : ISODate("2017-07-24T03:26:45.350Z")
}
]
}
注意:这会使用从meta集合中查找的内容文档来填充文件属性,因此您将丢失files.metaId
,但由于连接是从orders.files.metaId
到meta._id
文件子文档中_id的值与orders.files.metaId
相同。你可以看到这一点,如果你稍微修改一下命令:
db.getCollection('orders').aggregate([
// matches a single document in the orders collection
{'$match': {'_id': ObjectId("59784eb56149554af36a5666")}},
// unwinds orders.files so that it is no longer an array
{'$unwind': '$files'},
// performs the lookup on the meta collection and assigns the 'looked up' sub document to the attribute: "files"
{'$lookup': {
'localField': 'files.metaId',
'from': 'meta',
'foreignField': '_id',
'as': 'metaData'
}}
]);
这将返回:
{
"_id" : ObjectId("59784eb56149554af36a5666"),
"type" : "food order",
"isActive" : true,
"files" : {
"metaId" : ObjectId("59784e706149554af36a5621")
},
"metaData" : [
{
"_id" : ObjectId("59784e706149554af36a5621"),
"data" : "test data",
"createBy" : "testing",
"modifyBy" : "testing",
"modifyDate" : ISODate("2017-07-24T03:26:45.350Z"),
"createDate" : ISODate("2017-07-24T03:26:45.350Z")
}
]
}
在这files.metaId == metaData._id
。
这是工作,很棒:)。非常感谢你 。但有一点我们可以保留“”metaId“”的值,而不是删除它,也需要保留旧的_id字段 –
我已经更新了解决这一问题的答案:“我们可以保留”“metaId”“值,而不是去除它'。虽然我不清楚这个要求:“我需要保留旧的_id字段”。 – glytching