[问题]

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. HDU5692 Snacks DFS序 线段树

    去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...

  2. sql查询count 单独字段不同值

    1.单表查询 SELECT COUNT(CASE WHEN (字段=值列1) THEN reportstatus END) AS 已上报,COUNT(CASE WHEN (字段=值列0) THEN 字 ...

  3. ClassLoader家族

    DexClassLoader可以加载外部的dex,而PathClassLoader只能加载内部的dex 双亲委托 ClassLoader(ClassLoader parentLoader,boolea ...

  4. pacman 命令

    安装 pacman -S  删除 pacman -R  移除已安装不需要软件包 pacman -Rs  删除一个包,所有依赖 pacman -Rsc  升级包 pacman -Syu  查询包数据库 ...

  5. javascript宏任务和微任务

    函数 // 你不能改变一个函数的 name 属性的值, 因为该属性是只读的 var object = { // someMethod 属性指向一个匿名函数 someMethod: function() ...

  6. CMake结合Visual Studio中开发Qt应用程序注意事项

    Qt工程管理 个人比较偏爱于使用CMake来管理C++工程,因为只要编写一个CMakeLists.txt文件,就可以在Windows和Mac上生成各自的IDE工程.在Windows上, CMake自然 ...

  7. Xamarin Essentials教程检查网络连通性Connectivity

    Xamarin Essentials教程检查网络连通性Connectivity   网络连通性其实就是检测当前设备有没有连接网络.网络连通性在很多与网络相关的应用程序中会使用到.在Xamarin中如果 ...

  8. 366. Fibonacci

    描述 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2 ...

  9. C#常用字符串函数

    Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 CompareOrdinal 与Compare一样,但不考虑文化背景 Format 格式化包含各种值的字符串和如何格式化每 ...

  10. 潭州课堂25班:Ph201805201 django 项目 第十九课 文章主页数据库模型,前后台功能实现 (课堂笔记)

    -数据库模型设计 : 文章:新闻表: 字段:图片,标题,摘要,类型,作者,创建时间 标签表 评论表, 轮播图:外键,指向文章的外键表 在 utls 目录下创建 models.py  把其它模型常用的字 ...