[问题]

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

Project with Match in aggregate not working in mongodb的更多相关文章

  1. MongoDB——》聚合查询(project、match、limit、skip、unwind、group、sort)

    https://blog.csdn.net/weixin_43453386/article/details/85065043#1_testweightname_id_35 https://blog.c ...

  2. MongoDB的aggregate聚合

    聚合框架中常用的几个操作: $project:修改输入文档的结构.可以用来重命名.增加或删除域,也可以用于创建计算结果以及嵌套文档.(显示的列,相当遇sql 的) $match:用于过滤数据,只输出符 ...

  3. Mongodb笔记(三)user && aggregate && mapReduce

    版本:mongodb3.4. User: mongodb使用验证登录:默认不开启,mongod中使用--auth开启:  mongod -port=3000 --auth  : 基本方法: db.cr ...

  4. MongoDB.Driver 管道 Aggregate

    目前mongodb for C#这个驱动,在进行Aggregate时,只支持BsonDocument类型,也就是说,你的集合collection也必须返回的是BsonDocument,而实体类型是不可 ...

  5. Mongoose: aggregate聚合 $group使用说明

    aggregate聚合是通过管道操作实现的.聚合管道里的每一步输出,都会作为下一步的输入,每一步在输入文档执行完操作后生成输出文档. 聚合管道:  $project 修改输入文档的结构.可以用来重命名 ...

  6. MongoDB 聚合管道(Aggregation Pipeline)

    管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...

  7. MongoDB 聚合

    聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...

  8. MongoDB高级操作

    参考MongoDB菜鸟教程 一.$type操作符 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1   String 2   Object 3   Array 4   ...

  9. MongoDB基础教程系列--第七篇 MongoDB 聚合管道

    在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...

随机推荐

  1. 关于appium-doctor运行时提示不是内部或外部的命令

    1.一定要单独配置android_home (我之前是直接将D:\SDK\platform-tools;D:\SDK\tools;加到path里面会导致appnium-doctor运行时失败,原因为A ...

  2. List接口相对于Collection接口的特有遍历方法

    package com.hxl; import java.util.ArrayList; import java.util.List; public class Test { public stati ...

  3. Trident继承kafka

    1.Kafka涉及的类 上一个类是不透明事务 后一个是完全事务 2.启动服务 3..驱动类 重要的地方是修改了两个部分: 1.数据的来源是kafka 2.第二个是字段的Fields是str packa ...

  4. vue回调函数无法更改model的值

    data:{ isUpload:true, } 点击上传函数: getFile(event) { //选择图片 let eventId = event.target.id; let type= tes ...

  5. 爬虫3 requests基础2 代理 证书 重定向 响应时间

    import requests # 代理 # proxy = { # 'http':'http://182.61.29.114.6868' # } # res = requests.get('http ...

  6. cookie的基本操作

    设置,读取,删除 var odate=new Date(); odate.setDate(odate.getDate()+14); document.cookie='user=blue;expires ...

  7. POJ1062昂贵的聘礼(经典) 枚举区间 +【Dijkstra】

    <题目链接>                   昂贵的聘礼 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用1000 ...

  8. rock-paper-scissors

    rock-paper-scissors维护三个前缀和,然后注意顺序,最后做差来确定可行的答案,因为答案比较小,可以考虑这种暴力做法,像这种方案数可以++的题真的不多,如果想不出来特别优秀的想法,不妨简 ...

  9. 001.CDN概述

    一 互联网应用质量概述 1.1 互联网应用质量 互联网应用质量指标--QoE,其主要指标: 服务成功率:指用户所请求的服务成功完成的几率. 服务建立时间:指从服务请求到服务呈现所花费的时间,并且会因为 ...

  10. webstorm离线装载Material Theme UI

    首先说说需求,由于直接用webstorm听说VS挺火的,但是初恋的感觉是其他任何编辑器无法替代的 瞎说了一些话,新公司内网开发,用的是vscode,但是我还是喜欢用webstorm,连不上网,所以不能 ...