我在MongoDB中有一个名为"post"的集合,其中包含指向"author"的DBRef列表,而DBRef又将DBRef发送到集合"media"。
当我检索帖子时,我想获取帖子的整个对象,包括对象中的所有作者和媒体。
例如,这是我的收藏:
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"
}
}
]
}
您可以在中间阶段使用$project
更改格式
$unwind
可以使用选项。以下$unwind操作使用 preserveNullAndEmptyArrays 选项在输出中包含缺少 size(null 或空数组(的文档。
[
{ $unwind: { path: "$sizes", preserveNullAndEmptyArrays: true } }
]
这是文件