cond累加器是一元运算符

cond累加器是一元运算符

考虑这个管道。

let Pipeline = [
    { $match: {
    }},
    { $group: {
        workHours: { $sum: { $divide: [ { $subtract: ['$workTime.end', '$workTime.start'] } , { $multiply: [3600, 1000] }] }},
        breakHours: { $sum: { $divide: [ { $subtract: ['$breakTime.end', '$breakTime.start'] } , { $multiply: [3600, 1000] }] }},
        weekEndHours: {
            $cond: [
                { $or : [ { $eq : [{ $dayOfWeek : '$workTime.start' }, 1 ] }, {$eq : [{ $dayOfWeek : '$workTime.start' }, 7 ] }] },
                { $sum: { $divide: [ { $subtract: ['$workTime.end', '$workTime.start'] } , { $multiply: [3600, 1000] }] }},
                0
            ]
        }
    }},
];


我试图获得工作时间,休息时间和周末时间的总和。工作时间和休息时间工作正常。现在,我想在周末时段内使用条件,即如果日期中的某天是1或7,则在键中计算总小时数。

然后向我显示错误“ $ cond累加器是一元运算符”。我不知道我的群组查询出了什么问题。

最佳答案

得到了解决方案,并扩展了管道条件。

let Pipeline = [
    { $match: {
    }},
    { $group: {
        _id: '$memberId',
        workHours: { $sum: { $divide: [ { $subtract: ['$workTime.end', '$workTime.start'] } , { $multiply: [3600, 1000] }] }},
        breakHours: { $sum: { $divide: [ { $subtract: ['$breakTime.end', '$breakTime.start'] } , { $multiply: [3600, 1000] }] }},
        weekEndHours: {
            $sum: {
                $cond: [
                    { $or : [ { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 1 ] }, {$eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 7 ] }] },
                    { $divide: [ { $subtract: ['$workTime.end', '$workTime.start'] } , { $multiply: [3600, 1000] }] },
                    0
                ]
            }
        },
        weekDayHours: {
            $sum: {
                $cond: [
                    { $or : [
                            { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 2 ] },
                            { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 3 ] },
                            { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 4 ] },
                            { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 5 ] },
                            { $eq : [{ $dayOfWeek : { date: '$workTime.start', timezone: timezoneOffset}}, 6 ] }
                        ]
                    },
                    { $divide: [ { $subtract: ['$workTime.end', '$workTime.start'] } , { $multiply: [3600, 1000] }] },
                    0
                ]
            }
        },
    }},
    { $addFields:{
        totalWorkedHours: { $subtract: [ '$workHours', '$breakHours'] }
    }}
];

10-06 15:14