MongoDB中的$lookup提供了意外的数据结构结果
我试图理解我在MongoDB聚合中使用的MongoDB中的$lookup提供了意外的数据结构结果,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我试图理解我在MongoDB聚合中使用的$lookup为什么会产生这样的结果 首先,我的初始数据如下所示: "subscriptions": [ { "agency": "3dg2672f145d0598be095634", // This is an ObjectId "memberType": "primary" } ] { "from" : "agencies", "localField" : "subscriptions.
$lookup
为什么会产生这样的结果
首先,我的初始数据如下所示:
"subscriptions": [
{
"agency": "3dg2672f145d0598be095634", // This is an ObjectId
"memberType": "primary"
}
]
{
"from" : "agencies",
"localField" : "subscriptions.0.agency",
"foreignField" : "_id",
"as" : "subscriptions.0.agency"
}
"subscriptions": [
{
"agency": [
{
_id: <id-value>,
name: <name-value>,
address: <address-value>
}
],
"memberType": "primary"
}
]
现在,我要做的是一个简单的$lookup
,为ObjectId
引入相关数据,该数据当前作为“agency”字段的值填充
我试着做的是这样的$lookup
:
"subscriptions": [
{
"agency": "3dg2672f145d0598be095634", // This is an ObjectId
"memberType": "primary"
}
]
{
"from" : "agencies",
"localField" : "subscriptions.0.agency",
"foreignField" : "_id",
"as" : "subscriptions.0.agency"
}
"subscriptions": [
{
"agency": [
{
_id: <id-value>,
name: <name-value>,
address: <address-value>
}
],
"memberType": "primary"
}
]
因此,基本上我想做的是获取与ObjectId
ref相关的信息,并在这里填充它,而不是ObjectId
当前所在的位置
我所期望的结果是这样的:
"subscriptions": [
{
"agency": "3dg2672f145d0598be095634", // This is an ObjectId
"memberType": "primary"
}
]
{
"from" : "agencies",
"localField" : "subscriptions.0.agency",
"foreignField" : "_id",
"as" : "subscriptions.0.agency"
}
"subscriptions": [
{
"agency": [
{
_id: <id-value>,
name: <name-value>,
address: <address-value>
}
],
"memberType": "primary"
}
]
“订阅”:[
{
“机构”:[
{
_id:,
姓名:,
地址:
}
],
“成员类型”:“主要”
}
]
相反,我最终得到了这样的结果(我的“memberType”道具现在找不到了):
“订阅”:{
"0" : {
“机构”:[]
}
}
为什么这是$lookup
的结果,我如何获得我在这里寻找的数据结构
为了进一步澄清,在文档中,他们提到在作为数组字段的$lookup
之前使用$unwind
。但是在这种情况下,被指定为目标并由$lookup
替换的实际本地字段不是数组,而是在数组中。所以我不清楚问题出在哪里。您需要使用将“localField”与匹配到“foreignField”,然后再次回滚到数组
db.collection.aggregate([
{ "$unwind": "$subsciption" },
{ "$lookup": {
"from": Agency.collection.name,
"localField": "subsciption.agency",
"foreignField": "_id",
"as": "subsciption.agency"
}},
{ "$group": {
"_id": "$_id",
"memberType": { "$first": "$memberType" },
"subsciption": { "$push": "$subsciption" },
}}
])
基本上,OP需要的是在查找到另一个集合后将数据转换为所需的格式。假设有两个集合
C1
和C2
,其中C1
包含文档
{ "_id" : ObjectId("5b50b8ebfd2b5637081105c6"), "subscriptions" : [ { "agency" : "3dg", "memberyType" : "primary" } ] }
和C2
包含
{ "_id" : ObjectId("5b50b984fd2b5637081105c8"), "agency" : "3dg", "name" : "ABC", "address" : "1 some street" }
如果对数据库执行以下查询
db.C1.aggregate([
{$unwind: "$subscriptions"},
{
$lookup: {
from: "C2",
localField: "subscriptions.agency",
foreignField: "agency",
as: "subscriptions.agency"
}
}
])
我们得到了结果
{
"_id": ObjectId("5b50b8ebfd2b5637081105c6"),
"subscriptions": {
"agency": [{
"_id": ObjectId("5b50b984fd2b5637081105c8"),
"agency": "3dg",
"name": "ABC",
"address": "1 some street"
}],
"memberyType": "primary"
}
}
这与OP的预期非常接近
注意:可能存在一些边缘情况,但只要稍作调整,此解决方案就可以工作
您也可以使用
populate()
方法。或者你坚持使用$lookup
?我想知道为什么这个$lookup
会这样做。因为这实际上是一个mongo视图,而不是一个集合,所以我不确定是否可以实际使用populate()。您需要$unwind
您的SubscriptionTanks,但您能否帮助我了解原因-因为目标本地字段不是数组?在本例中,$unwinding first将为每个订阅提供一个文档--这不是我想要的。那我该怎么解决呢?谢谢。这是关键——使用$group和$push将订阅作为一个数组进行维护。