我有一个类似的查询
let issuerPaid = await ChallengeModel.aggregate([
{ $match: {voucher_paid_by: 'issuer', challenge_by_appuser: mongoose.Types.ObjectId(req.body.user_id)} },
{ $lookup: {from: 'vouchers',localField: 'voucher_id',foreignField: '_id',as: 'voucher'} },
{ $lookup: {from: 'appusers',localField: 'challenge_loser',foreignField: '_id',as: 'challenge_loser'} },
]);
结果就像
{
"issuerPaid": [
{
"_id": "5b86b18afa04445392ff6542",
"voucher_id": "5b7cfa381496bea7d6c5bea4",
"voucher_paid_by": "loser",
"challenge_loser": [
{
"_id": "5b75643c0a97791bcc9ed64c",
"status": "1",
"last_name": "Gmail",
"first_name": "Vishwas",
"created_at": "2018-08-16T11:47:08.475Z",
"updated_at": "2018-08-28T12:54:45.462Z"
}
],
"voucher": [
{
"_id": "5b7cfa381496bea7d6c5bea4",
"voucher_name": "Walora",
"voucher_description": "Where fashion meets fitness! Get a 20% discount on all active wear purchases",
"voucher_disclaimer": "",
"voucher_validity": "2018-10-31 00:00:00.666",
"voucher_logo": "/vouchers/5b7cf73ccee4b5b7cf73ccee85.jpg",
"status": "1",
"created_at": "2018-08-22T05:52:56.156Z",
"updated_at": "2018-08-22T05:52:56.156Z"
}
]
}
]
}
但是它返回appusers
的所有字段。但是我只想要appusers
的{{1}}和first_name
。
我尝试last_name
和$unwind
仅$project
和first_name
,但是当没有last_name
时,它返回0条记录,而我想要返回记录(如果不存在)。因此我无法使用voucher_id
。
我希望结果看起来像
$unwind
现在我在计划特定领域时遇到了麻烦。
请帮助。