SaisJS - $match 中的条件不起作用

SaisJS - The condition in $match is not working

我的代码是这样的:

User.native(function (err, collection) {
        if (err) throw err;
        collection.aggregate([
            {
                $match: {
                    id: req.params.id,
                    createdAt: {">=": start, "<=": end}
                }
            },
            {
                $group: {
                    _id: "$class",
                    count: {$sum: 1},
                    jointime: {$max: "$endtime"}
                }
            }
        ]

createdAt是我添加的用于按时间过滤集合的,但是在我这样做之后,没有返回任何结果。

我以为是日期类型导致的,因为createdAt是MongoDB中的日期。但是当我尝试这样做时,我可以获得正确的数据。

User.find({}, {id: req.params.childid, createdAt: {">=": start, "<=": end}}, function (err, result) {
        console.log("CreatedAt: " + result[0].createdAt);
        console.log("Result length: " + result.length);
    });

所以,我认为这与日期无关。 aggregate有什么不同吗?

您应该在 $match 运算符中使用 $gte$lte 而不是“>=”和“<=”。

 $match: {
         id: req.params.id,
         createdAt: {$gte: start, $lte: end}
 }