gpt4 book ai didi

mongodb - 蒙戈错误: The $cond accumulator is a unary operator

转载 作者:行者123 更新时间:2023-12-03 15:59:39 29 4
gpt4 key购买 nike

考虑这个管道。

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'] }
}}
];

关于mongodb - 蒙戈错误: The $cond accumulator is a unary operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52963085/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com