【问题标题】:MongoDB & Node.js - How to sum a field with $eq and $cond where the expression is an array of objectsMongoDB 和 Node.js - 如何用 $eq 和 $cond 对字段求和,其中表达式是对象数组
【发布时间】:2020-03-28 00:52:04
【问题描述】:

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

中是否有这些属性
$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();```

【问题讨论】:

  • 只是检查为什么{ $eq: ['$dwellingDetail.publicationType', "Alquiler"] } 两个字段sumAlquilerWpsumAlquilerMail 的条件相同?
  • 它工作正常还是仍有问题?

标签: node.js mongodb mongodb-query aggregation-framework


【解决方案1】:

您不能将['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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2021-01-09
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    相关资源
    最近更新 更多