Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果查找id位于嵌入式文档mongodb中,如何高效地实现连接?_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

如果查找id位于嵌入式文档mongodb中,如何高效地实现连接?

如果查找id位于嵌入式文档mongodb中,如何高效地实现连接?,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,考虑一个场景,用户可以订阅许多频道,而频道属于出版商 基本上有三个实体 1:用户 2:频道 3:出版商 下面是以db格式保存的示例数据: { "_id" : ObjectId("5a7428b7408437d7155bbde9"), "name" : "some subscriber who is a user ", "channelSubscription" : { "5a740c88408437d7155bbdca" : { "_id" : "5a74

考虑一个场景,
用户
可以订阅许多
频道
,而
频道
属于
出版商

基本上有三个实体

1:用户

2:频道

3:出版商

下面是以db格式保存的示例数据:

{
  "_id" : ObjectId("5a7428b7408437d7155bbde9"),
  "name" : "some subscriber who is a user ",
    "channelSubscription" : {
      "5a740c88408437d7155bbdca" : {
      "_id" : "5a740c88408437d7155bbdca",
      "channel_name" : "1st channel",
      "channelBelongsTo" : "publisher._id" // replace id with publisher details
   },

    "5a7411dc408437d7155bbdcb" : {
       "_id" : "5a7411dc408437d7155bbdcb",
       "channel_name" : "2nd channel",
       "channelBelongsTo" : "someOtherPublisher._id" // replace id with publisher details
     }
   }
}

我如何在此处进行连接以获取发布者的详细信息,以便
channelBelongsTo
可以拥有发布者的详细信息。您需要在此处使用聚合框架以获得所需的结果。聚合管道最初应该创建一个新字段,该字段是hashmap的键/值对数组,可以使用管道步骤和$objectToArray操作符。因此,您的第一个管道步骤是:

db.user.aggregate([
    {
       "$addFields": {
           "subscriptions": { "$objectToArray": "$channelSubscription" }
       } 
    }
])
产生

{
    "_id" : ObjectId("5a7428b7408437d7155bbde9"),
    "name" : "some subscriber who is a user ",
    "channelSubscription" : {
        "5a740c88408437d7155bbdca" : {
            "_id" : "5a740c88408437d7155bbdca",
            "channel_name" : "1st channel",
            "channelBelongsTo" : "1"
        },
        "5a7411dc408437d7155bbdcb" : {
            "_id" : "5a7411dc408437d7155bbdcb",
            "channel_name" : "2nd channel",
            "channelBelongsTo" : "2"
        }
    },
    "subscriptions" : [ 
        {
            "k" : "5a740c88408437d7155bbdca",
            "v" : {
                "_id" : "5a740c88408437d7155bbdca",
                "channel_name" : "1st channel",
                "channelBelongsTo" : "1"
            }
        }, 
        {
            "k" : "5a7411dc408437d7155bbdcb",
            "v" : {
                "_id" : "5a7411dc408437d7155bbdcb",
                "channel_name" : "2nd channel",
                "channelBelongsTo" : "2"
            }
        }
    ]
}

对于加入,您需要附加管道步骤,该步骤获取发布者的详细信息,如下所示:

db.user.aggregate([
    {
       "$addFields": {
           "subscriptions": { "$objectToArray": "$channelSubscription" }
       } 
    },
    {
        "$lookup": {
            "from": "publisher",
            "localField": "subscriptions.v.channelBelongsTo",
            "foreignField": "_id",
            "as": "publishers"
        }
    }
])

没用的出版商现在是一片空白,我错了,
“localField”:“subscriptions.v.channelBelongsTo”
存储为
字符串,而不是
对象ID
谢谢!!