Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Mongodb 使用嵌套DBREF的Mongo查找_Mongodb_Dbref - Fatal编程技术网

Mongodb 使用嵌套DBREF的Mongo查找

Mongodb 使用嵌套DBREF的Mongo查找,mongodb,dbref,Mongodb,Dbref,我在MongoDB中有一个名为“post”的集合,其中包含指向“author”的DBRef列表,而“author”又包含指向集合“media”的DBRef 当我检索文章时,我想获取整个post对象,包括对象中的所有作者和媒体 以下是我的收藏: post: { "_id" : ObjectId("5d7db7af49c5e277395871bd"), "authors" : [ DBRef("author", ObjectId("5d7daaab49c5e27739

我在MongoDB中有一个名为“post”的集合,其中包含指向“author”的DBRef列表,而“author”又包含指向集合“media”的DBRef

当我检索文章时,我想获取整个post对象,包括对象中的所有作者和媒体

以下是我的收藏:

post:
{
    "_id" : ObjectId("5d7db7af49c5e277395871bd"),
    "authors" : [
        DBRef("author", ObjectId("5d7daaab49c5e277395871ba")),
        DBRef("author", ObjectId("5d7daaab49c5e277395871bb"))
    ]
}

author:
[{
    "_id" : ObjectId("5d7daaab49c5e277395871ba"),
    "name" : "author 1",
    "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b8"))
},
{
    "_id" : ObjectId("5d7daaab49c5e277395871bb"),
    "name" : "author 2",
    "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b9"))
}]

media:
[{
    "_id" : ObjectId("5d7daa2049c5e277395871b8"),
    "url" : "http://image.com/1",
    "type" : "png"
},
{
    "_id" : ObjectId("5d7daa2049c5e277395871b9"),
    "url" : "http://image.com/2",
    "type" : "png"
}]
我希望查询得到以下结果:

post:
{
    "_id" : ObjectId("5d7db7af49c5e277395871bd"),
    "authors" : [
            {
                "_id" : ObjectId("5d7daaab49c5e277395871ba"),
                "name" : "author 1",
                "media" : {
                    "_id" : ObjectId("5d7daaab49c5e277395871ba"),
                    "name" : "author 1",
                    "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b8"))
                }
            },
            {
                "_id" : ObjectId("5d7daaab49c5e277395871bb"),
                "name" : "author 2",
                "media" : {
                    "_id" : ObjectId("5d7daaab49c5e277395871bb"),
                    "name" : "author 2",
                    "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b9"))
                }
            }
    ]
}

到目前为止,我有以下查询,其中包括一篇文章中的作者,但我需要帮助检索作者中的媒体

current query:

db.post.aggregate([
{
    $project: { 
        authors: {
          $map: { 
             input: { 
                  $map: {
                      input:"$authors",
                      in: {
                           $arrayElemAt: [{$objectToArray: "$$this"}, 1]
                      },
                  }
             },
             in: "$$this.v"}},
        }

}, 
{
    $lookup: {
        from:"author", 
        localField:"authors",
        foreignField:"_id", 
        as:"authors"
    }
},
])

current result:

{
    "_id" : ObjectId("5d7db7af49c5e277395871bd"),
    "authors" : [
        {
            "_id" : ObjectId("5d7daaab49c5e277395871ba"),
            "name" : "author 1",
            "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b8"))
        },
        {
            "_id" : ObjectId("5d7daaab49c5e277395871bb"),
            "name" : "author 2",
            "media" : DBRef("media", ObjectId("5d7daa2049c5e277395871b9"))
        }
    ]
}

尝试使用
$unwind
,然后按id使用
$goup

你可以这样做

db.post.aggregate([{
    $project: {
        authors: {
            $map: {
                input: {
                    $map: {
                        input: "$authors",
                        in: {
                            $arrayElemAt: [{
                                $objectToArray: "$$this"
                            }, 1]
                        },
                    }
                },
                in: "$$this.v"
            }
        },
    }
}, {
    $lookup: {
        from: "author",
        localField: "authors",
        foreignField: "_id",
        as: "authors"
    }
}, {
    $unwind: "$authors"
}, {
    $project: {
        "authors.media": {
            $arrayElemAt: [{
                $objectToArray: "$authors.media"
            }, 1]
        },
        "authors._id": 1,
        "authors.name": 1
    }
}, {
    $lookup: {
        from: "media",
        localField: "authors.media.v",
        foreignField: "_id",
        as: "authors.media"
    }
}, {
    $unwind: {
        path: "$authors.media",
        preserveNullAndEmptyArrays: true
    }
}, {
    $group: {
        _id: "$_id",
        authors: {
            $push: "$authors"
        }
    }
}]).pretty()

结果如下


    "_id" : ObjectId("5d7db7af49c5e277395871bd"),
    "authors" : [
        {
            "_id" : ObjectId("5d7daaab49c5e277395871ba"),
            "name" : "author 1",
            "media" : {
                "_id" : ObjectId("5d7daa2049c5e277395871b8"),
                "url" : "http://image.com/1",
                "type" : "png"
            }
        },
        {
            "_id" : ObjectId("5d7daaab49c5e277395871bb"),
            "name" : "author 2",
            "media" : {
                "_id" : ObjectId("5d7daa2049c5e277395871b9"),
                "url" : "http://image.com/2",
                "type" : "png"
            }
        }
    ]
}

您可以在<4/P >中更改格式>代码> $Project < /Cord> 还有什么

$unwind
可与选项一起使用。下面的$unwind操作使用PreserveNullAndEmptyArray选项在输出中包括缺少sizes字段、null或空数组的文档

[
   { $unwind: { path: "$sizes", preserveNullAndEmptyArrays: true } }
]

这是您的作者和媒体的
[]
是一个或两个文档您的收藏
作者
也包含DBref,它显示为解析again@HbnKing是的,这就是我需要帮助的地方,特别是在author集合中解析DBRef。