[问题]

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. HTML中鼠标滚轮事件onmousewheel

    IE/Opera属于同一类型,使用attachEvent即可添加滚轮事件. /*IE注册事件*/ if(document.attachEvent){ document.attachEvent('onm ...

  2. Noj - 在线强化训练1

      1445 A 求n个整数的和   1564 B 判断一个数是否是完全数   1011 C 判素数(Prime number)   1566 D 输入一组整数,找出最小值   1200 E 判断三角 ...

  3. 使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form

    使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form 开发环境: Wing IDE 6.1 步骤1: 打开 Wing IDE,创建一个新的 pr ...

  4. hive有关函数

    1.窗口函数2015年4月份购买过的顾客及总人数 select distinct name,count(1) over() as cnt from test_window_yfwhere substr ...

  5. TF之BN:BN算法对多层中的每层神经网络加快学习QuadraticFunction_InputData+Histogram+BN的Error_curve

    # 23 Batch Normalization import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...

  6. VS2017动态链接库(.dll)的生成与使用

    转 https://blog.csdn.net/m0_37170593/article/details/76445972 这里以VS2017为例子,讲解一下动态链接库(.dll)的生成与使用. 一.动 ...

  7. Django之Models(一)

    Django之Models(一) 目录 数据库的配置 模型代码与参数解析 ORM对单表的增删改查 查询的补充 数据库配置 django默认支持sqlite,mysql, oracle,postgres ...

  8. Linux学习之RPM包管理-rpm命令管理(十六)

    Linux学习之RPM包管理-rpm命令管理 目录 简介 RPM包依赖性 包全名与包名 rpm软件包安装 rpm软件包卸载 升级降级rpm软件包 rpm软件包的查询 rpm软件包校验 简介 RPM是R ...

  9. Alpha(3/10)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  10. 解决WPF导入图片不显示的问题

    想在XAML中使用一张图片,得先将其添加到工程中, 方法是: 在项目中双击Resources.resx,选择图像,在添加资源的下拉菜单中选择添加现有文件,然后选择文件,添加图片进来后可以在Resour ...