随笔 - 547  文章 - 213 评论 - 417 阅读 - 107万

 

[问题]


2down votefavorite

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

    });

which gives me following output:

[ { _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);

    });

which gives me following output:

 []

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?

Here is the sample data

{

    "_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

}

 

 

[回答]

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.

 

 

也就是说,project的字段中必须包含match中用到的字段.

 

来自: https://stackoverflow.com/questions/33752817/project-with-match-in-aggregate-not-working-in-mongodb

posted on   今夜太冷  阅读(325)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2012-07-18 入乡随俗:ASP.NET的本地化(Localization)简介
点击右上角即可分享
微信分享提示