我想在项目汇总中获取单个项目的投标清单。
但是当我运行Mongodb聚合时,如果找不到项目投标的任何结果,它将返回以下结果:
[ { _id: 5b69f6afa1ad1827cc9e1dc6,
projectID: 100029,
bidsArray: [ { freelanceArray: {} } ]
} ]
但是,如果找不到相关的出价,我想返回空的 bidsArray (这样):
[ { _id: 5b69f6afa1ad1827cc9e1dc6,
projectID: 100029,
bidsArray: []
} ]
这是我的总数:
[
{
$match: {
projectID: projectID
}
},
{
$lookup: {
from: "bids",
localField: "projectID",
foreignField: "projectID",
as: "bidsArray"
}
},
{
$unwind: {
path: "$bidsArray",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "users",
localField: "bidsArray.freelanceID",
foreignField: "userID",
as: "freelanceArray"
}
},
{
$unwind: {
path: "$freelanceArray",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: "$_id",
projectID: { $first: "$projectID" },
bidsArray: {
$addToSet: {
bidID: "$bidsArray.bidID",
daysToDone: "$bidsArray.daysToDone",
freelanceArray: {
userID: "$freelanceArray.userID",
username: "$freelanceArray.username",
publicName: "$freelanceArray.publicName",
}
}
}
}
},
{
$project: {
projectID: 1,
bidsArray: {
bidID: 1,
daysToDone: 1,
freelanceArray: {
userID: 1,
username: 1,
publicName: 1,
}
}
}
}
]