我有2个收藏
material_pack
{
"_id" : ObjectId("5e43bd417b5590b641d2f408"),
"title" : "untitle",
"ref" : [
{"id" : ObjectId("5e43bd517b5590b641d2f409")},
{"id" : ObjectId("5e43bd517b5590b641d2f40a")},
{"id" : ObjectId("5e43bd517b5590b641d2f40b")}
]
和 材料
[
{
"_id" : ObjectId("5e43bd517b5590b641d2f409"),
"mine_type" : "image/gif"
},
{
"_id" : ObjectId("5e43bd517b5590b641d2f40a"),
"mine_type" : "image/jpeg"
},
{
"_id" : ObjectId("5e43bd517b5590b641d2f40b"),
"mine_type" : "image/jpeg"
},
]
我想要的是将他们与此代码结合在一起
db.material_pack.aggregate([
{
$lookup:
{
from: 'material',
localField: 'ref.id',
foreignField: '_id',
as: "items_document"
}
}
])
但items_document字段的结果为[]
我的结果:
{
"_id" : ObjectId("5e43bd417b5590b641d2f408"),
"title" : "untitle",
"content" : "",
"categories" : [],
"ref" : [
{
"id" : ObjectId("5e43bd517b5590b641d2f409")
},
{
"id" : ObjectId("5e43bd517b5590b641d2f40a")
},
{
"id" : ObjectId("5e43bd517b5590b641d2f40b")
}
],
"items_document" : [ ]
}
我想要这样的结果:
{
"_id" : ObjectId("5e43bd417b5590b641d2f408"),
"title" : "untitle",
"content" : "",
"categories" : [],
"ref" : [
{
"id" : ObjectId("5e43bd517b5590b641d2f409")
},
{
"id" : ObjectId("5e43bd517b5590b641d2f40a")
},
{
"id" : ObjectId("5e43bd517b5590b641d2f40b")
}
],
"items_document" : [
{
"_id" : ObjectId("5e43bd517b5590b641d2f409"),
"mine_type" : "image/gif",
...
},
{
"_id" : ObjectId("5e43bd517b5590b641d2f40a"),
"mine_type" : "image/jpeg",
...
},
{
"_id" : ObjectId("5e43bd517b5590b641d2f40b"),
"mine_type" : "image/jpeg",
...
}
]
}
答案 0 :(得分:-1)
你好Apiwut Trusphimai, 请检查此Mongo Play,它可能会对您有所帮助。 我已经使用了您的代码和集合,并且无需对代码进行任何更改,即可很好地查看我给定的链接。