在将两个MongoDB集合结合在一起时遇到问题。
收藏集A:
{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
{
"userid" : ObjectId("5c751043b3da4a8e63b7630b"),
"Size" : "Small",
"Status" : "New",
{
"userid" : ObjectId("5c751043b3da4a8e63b7630b"),
"Size" : "Medium",
"Status" : "New",
},
]
}
{
"_id" : ObjectId("5c7e45ce875ea5548de25723"),
"Ironing" : [
{
"userid" : ObjectId("5c751043b3da4a8e63b76xxx"),
"NumeroCapi" : "4",
"Status" : "New",
},
{
"userid" : ObjectId("5c751043b3da4a8e63b76xxx"),
"NumeroCapi" : "4",
"Status" : "New",
},
]
}
}
集合B:
{
{ "_id" : ObjectId("5c751043b3da4a8e63b7630b"), "Username" : "Marco"}
{ "_id" : ObjectId("5c7510d1b3da4a8e63b7630c"), "Username" :"Max"}
}
我只希望部分成功时就加入userid并仅从信息中提取用户名。其次,我希望此查询加入Collection A中的所有顶级字段,例如Autos,Ironing等。
这是我的查询...部分仅适用于汽车:(
db.rental.aggregate(
[
{
$unwind: "$Autos"
},
{
$lookup:
{
from: "user",
localField: "Autos.userid",
foreignField: "_id",
as: "Autos.userinfo"
}
},
{
$group:
{
_id: "$_id",
Autos:{$push:"$Autos"}
}
},
]
).pretty()
结果:
{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
{
"userid" : ObjectId("5c751043b3da4a8e63b7630b"),
"Size" : "Small",
"Status" : "New",
"userinfo" : [
{
"_id" : ObjectId("5c751043b3da4a8e63b7630b"),
"Username" : "Marco",
}
]
},
}
必填结果:
{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
{
"userid" : ObjectId("5c751043b3da4a8e63b7630b"),
"Size" : "Small",
"Status" : "New",
"Username" : "Marco",
].
"Ironing" : [
{
"userid" : ObjectId("5c751043b3da4a8e63b7630b"),
"Status" : "New",
"Numericapi":5,
"Username" : "Marco",
]
},
}
一束谢谢...