聚合匹配的项目在 mongodb 中不起作用 [英] Project with Match in aggregate not working in mongodb

查看:16
本文介绍了聚合匹配的项目在 mongodb 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch data based on some match condition. First I've tried this: Here ending_date is full date format

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

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

Now I want to check year also, so I am trying this:

    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,因此当我匹配年份时,它应该排在数组中.但我得到空数组.为什么会这样?

as you can see, _id has 2015 as a year, so when I match year it should be come in array. But I am getting null array. Why this?

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

Is there any other way to match only year form whole datetime?

这是示例数据

{
    "_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 中使用的字段.要快速修复,请改用此查询:

You forgot to project the fields that you're using in $match and $group later on. For a quick fix, use this query instead:

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); 
});

话虽如此,布雷克七号在她的回答中解释了如何进行更好的查询.我认为你应该尝试使用她的方法.

That said, Blakes Seven explained how to make a better query in her answer. I think you should try and use her approach instead.

这篇关于聚合匹配的项目在 mongodb 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆