$ gt中的三元运算符

时间:2019-07-10 10:24:25

标签: mongodb mongoose

以下代码段遇到的问题是$ cond运算符似乎不起作用。当我将其完全替换为值8时,它就像一种魅力。注意:我只是想看看$ cond如何与$ gt一起工作。最终,布尔条件将与$ group中的日期字段有关(我将尝试检查日期是否为工作日)。

db.getCollection("intervals-time").aggregate([
    {
            $addFields: {
                convertedTime: {
                    $toDouble: "$time"
                }
            }
        },
        {
            $group: {
                _id:  {
                    personid: "$personid",
                    date: {
                        month: { $month: "$date" },
                        day: { $dayOfMonth: "$date" },
                        year: { $year: "$date"}
                    }
                },
                total: {
                    $sum: "$convertedTime"
                },

            }
        },
        {
            $match: { total: { $gt : {$cond:[true,8,5]}}}

        }])

2 个答案:

答案 0 :(得分:1)

您需要使用$expr来实现

{ "$match": { "$expr": { "$gt": ["$total", { "$cond": [true, 8, 5] }] }}}

答案 1 :(得分:1)

不能在$ match运算符内使用$ cond。 $ match仅用于过滤文档,而使用$ cond时,您将根据某些条件设置/更改某些值。可以在$ project操作中使用$ cond,而不能在$ match中使用。

相关问题