首页 > 解决方案 > MongoDB & Node.js - 如何用 $eq 和 $cond 对字段求和,其中表达式是对象数组

问题描述

我们有这个代码。请检查我们在

$project:
sumVentaWp
sumVentaMail
sumAlquilerWp
sumAlquilerMail

主要目标是得到一个条件的总和,问题是它dwellingDetaildwellingDetail.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();```

标签: node.jsmongodbmongodb-queryaggregation-framework

解决方案


您不能与之比较['Venta']'Venta'而是可以使用$in

{
    $project: {
        agencyname: '$name',
        agencyId: '$_id',
        sumVentaWp: {
            $sum: { $cond: [{ $in: [ "Venta","$dwellingDetail.publicationType"] }, then: 1, else: 0 ] }
        },
        sumVentaMail: {
            $sum: { $cond: [{ $in: [ "Venta", '$dwellingDetail.publicationType'] }, 1, 0] }
        },
        sumAlquilerWp: {
            $sum: { $cond: [{ $in: ["Alquiler",'$dwellingDetail.publicationType'] }, 1, 0] }
        },
        sumAlquilerMail: {
            $sum: { $cond: [{ $in: [ "Alquiler",'$dwellingDetail.publicationType'] }, 1, 0] }
        },
        pubType: '$dwellingDetail.publicationType',

        whatsappcounter: {
            $sum: '$inquiries.whatsappcounter'
        },
        mailcounter: { $sum: '$inquiries.mailcounter' },
        'detail._id': '0'
    }
}

注意:以防万一您有多个条件要检查,您可以尝试使用$switch


推荐阅读