у нас есть этот код. Пожалуйста, убедитесь, что у нас есть эти свойства в
$project:
sumVentaWp
sumVentaMail
sumAlquilerWp
sumAlquilerMail
Основная цель - получить сумму с условием, проблема в том, что dwellingDetail
и dwellingDetail.publicationType
являются массивами объектов, и я думаю, что мы можем не сравнить его с "Venta" (поэтому он всегда возвращает 0). $eq: ["$dwellingDetail.publicationType", "Venta"]
Есть ли способ проверить определенный объект в dwellingDetail?
{
$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();```