mongodb - 具有 Match 的项目在 mongodb 中不起作用

标签 mongodb mongodb-query aggregation-framework

我正在尝试根据某些匹配条件获取数据。首先我试过这个: 这里ending_date是完整的日期格式

    Offer.aggregate([ 
    { 
        $match: { 
            carer_id : req.params.carer_id,
            status : 3
        }
    },
    {
        $group : {
            _id : { year: { $year : "$ending_date" }, month: { $month : "$ending_date" }}, 
            count : { $sum : 1 }
        }
    }], 
    function (err, res)
    { if (err) ; // TODO handle error 
         console.log(res); 
    });

这给了我以下输出:

[ { _id: { year: 2015, month: 11 }, count: 2 } ]

现在我也想检查年份,所以我正在尝试:

    Offer.aggregate([
    {
        $project: {
            myyear: {$year: "$ending_date"}
        }
    },
    { 
        $match: { 
            carer_id : req.params.carer_id,
            status : 3,
            $myyear : "2015"
        }
    },
    {
        $group : {
            _id : { year: { $year : "$ending_date" }, month: { $month : "$ending_date" }}, 
            count : { $sum : 1 }
        }
    }], 
    function (err, res)
    { if (err) ; // TODO handle error 
         console.log(res); 
    });

这给了我以下输出:

 []

如您所见,_id 的年份为 2015,所以当我匹配年份时,它应该以数组形式出现。但我得到空数组。为什么会这样?

有没有其他方法可以只匹配整个日期时间的年份?

这是样本数据

{
    "_id": {
        "$oid": "56348e7938b1ab3c382d3363"
    },
    "carer_id": "55e6f647f081105c299bb45d",
    "user_id": "55f000a2878075c416ff9879",
    "starting_date": {
        "$date": "2015-10-15T05:41:00.000Z"
    },
    "ending_date": {
        "$date": "2015-11-19T10:03:00.000Z"
    },
    "amount": "850",
    "total_days": "25",
    "status": 3,
    "is_confirm": false,
    "__v": 0
}
{
    "_id": {
        "$oid": "563b5747d6e0a50300a1059a"
    },
    "carer_id": "55e6f647f081105c299bb45d",
    "user_id": "55f000a2878075c416ff9879",
    "starting_date": {
        "$date": "2015-11-06T04:40:00.000Z"
    },
    "ending_date": {
        "$date": "2015-11-16T04:40:00.000Z"
    },
    "amount": "25",
    "total_days": "10",
    "status": 3,
    "is_confirm": false,
    "__v": 0
}

最佳答案

您忘记投影您稍后在 $match$group 中使用的字段。如需快速修复,请改用此查询:

Offer.aggregate([
{
    $project: {
        myyear: { $year: "$ending_date" },
        carer_id: 1,
        status: 1,
        ending_date: 1
    }
},
{ 
    $match: { 
        carer_id: req.params.carer_id,
        myyear: 2015,
        status: 3
    }
},
{
    $group: {
        _id: {
            year: { $year: "$ending_date" },
            month: { $month: "$ending_date" }
        }, 
        count: { $sum: 1 }
    }
}], 
function (err, res)
{
    if (err) {} // TODO handle error 
    console.log(res); 
});

也就是说,Blakes Seven解释了如何在她的回答中做出更好的查询。我认为您应该尝试改用她的方法。

关于mongodb - 具有 Match 的项目在 mongodb 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752817/

相关文章:

node.js - Mongo 仅以 ISO_8601 格式存储日期,而不是 unix

javascript - 根据feathersJs api中自定义ID名称从mongo数据库获取模型数据

java - 使用java在mongodb中自动递增序列

mongodb - MongoDB 中的项目对象存在 bool 值

MongoDB:解析命令行时出错:无法识别的选项 '--sslMode'

mongodb - 没有数组的嵌入文档?

javascript - 如何使用 Node.js 在 MongoDB 中查找下一个文档

mongodb - 加速 MongoDB 聚合

mongodb count 每个字段/键的不同值的数量

mongodb - 使用 $switch 将分数添加到 MongoDB 聚合