【发布时间】:2020-03-28 00:52:04
【问题描述】:
我们有这个代码。请检查我们在
中是否有这些属性$project:
sumVentaWp
sumVentaMail
sumAlquilerWp
sumAlquilerMail
主要目标是得到一个条件的总和,问题是dwellingDetail 和dwellingDetail.publicationType 是对象数组,我认为我们不能将它与“Venta”进行比较(这就是为什么它总是返回 0 )。
$eq: ["$dwellingDetail.publicationType", "Venta"]
有没有办法检查住宅详细信息中的特定对象?
{
$lookup: {
localField: '_id',
from: 'inquiries', //the collection name, (bad)before i had Phrase as the model
foreignField: 'agencyId',
as: 'inquiries'
},
},
{
$lookup: {
localField: 'inquiries.dwellingId',
from: 'dwelling', //the collection name, (bad)before i had Phrase as the model
foreignField: '_id',
as: 'dwellingDetail'
},
},
{
$match: {
'deleted': false,
}
},
{ $sort: { name: 1 } },
{
$project: {
agencyname: '$name',
agencyId: '$_id',
sumVentaWp: {
$sum: { $cond: { if: { $eq: ["$dwellingDetail.publicationType", "Venta"] }, then: 1, else: 0 } }
},
sumVentaMail: {
$sum: { $cond: [{ $eq: ['$dwellingDetail.publicationType', "Venta"] }, 1, 0] }
},
sumAlquilerWp: {
$sum: { $cond: [{ $eq: ['$dwellingDetail.publicationType', "Alquiler"] }, 1, 0] }
},
sumAlquilerMail: {
$sum: { $cond: [{ $eq: ['$dwellingDetail.publicationType', "Alquiler"] }, 1, 0] }
},
pubType: '$dwellingDetail.publicationType',
whatsappcounter: {
$sum: '$inquiries.whatsappcounter'
},
mailcounter: { $sum: '$inquiries.mailcounter' },
'detail._id': '0'
}
}
]).exec();```
【问题讨论】:
-
只是检查为什么
{ $eq: ['$dwellingDetail.publicationType', "Alquiler"] }两个字段sumAlquilerWp和sumAlquilerMail的条件相同? -
它工作正常还是仍有问题?
标签: node.js mongodb mongodb-query aggregation-framework