Project with Match in aggregate not working in mongodb
[问题]
|
2down votefavorite I am trying to fetch data based on some match condition. Offer.aggregate([ { $match: { carer_id : req.params.carer_id, status : 3 } }, { $group : { _id : { year: { $year : "$ending_date" }, month: count : { $sum : 1 } } }], function (err, res) { if (err) ; // TODO handle error console.log(res); }); which gives me following output: [ { _id: { 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: 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 Is there any other way to match only year form whole 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 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的更多相关文章
- MongoDB——》聚合查询(project、match、limit、skip、unwind、group、sort)
https://blog.csdn.net/weixin_43453386/article/details/85065043#1_testweightname_id_35 https://blog.c ...
- MongoDB的aggregate聚合
聚合框架中常用的几个操作: $project:修改输入文档的结构.可以用来重命名.增加或删除域,也可以用于创建计算结果以及嵌套文档.(显示的列,相当遇sql 的) $match:用于过滤数据,只输出符 ...
- Mongodb笔记(三)user && aggregate && mapReduce
版本:mongodb3.4. User: mongodb使用验证登录:默认不开启,mongod中使用--auth开启: mongod -port=3000 --auth : 基本方法: db.cr ...
- MongoDB.Driver 管道 Aggregate
目前mongodb for C#这个驱动,在进行Aggregate时,只支持BsonDocument类型,也就是说,你的集合collection也必须返回的是BsonDocument,而实体类型是不可 ...
- Mongoose: aggregate聚合 $group使用说明
aggregate聚合是通过管道操作实现的.聚合管道里的每一步输出,都会作为下一步的输入,每一步在输入文档执行完操作后生成输出文档. 聚合管道: $project 修改输入文档的结构.可以用来重命名 ...
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- MongoDB高级操作
参考MongoDB菜鸟教程 一.$type操作符 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1 String 2 Object 3 Array 4 ...
- MongoDB基础教程系列--第七篇 MongoDB 聚合管道
在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...
随机推荐
- ghithub中PHPOffice/PHPWord的学习
1.概念:PHPWord是用纯PHP提供了一组类写入和从不同的文档格式的文件阅读库.PHPWord的当前版本支持微软的Office Open XML(OOXML或处理OpenXML),用于Office ...
- easyui 信息提示
/*消息提示begin*/jQuery.Info = function (msg) { $.messager.alert("温馨提示", msg, "info" ...
- 069 在SparkStreaming的窗口分析
一:说明 1.图例说明 ---------------------------------------------------------------------------------------- ...
- 003 python中的内置函数
一:如何查看内置函数 1.命令 dir(__builtins__) 2.效果 二:具体的用法 1.input 简单使用: 2.type 返回变量的类型 3.str 将类型转变为字符串 4.isinst ...
- Codeforces 1105C Ayoub and Lost Array (计数DP)
<题目链接> 题目大意: 有一个长度为 n 的数列的未知数列,数列的每一个数的值都在区间 [l,r] 的范围内.现在问你能够构成多少个这样的数组,使得数组内的所有数的和能够被 3 整除. ...
- Codeforces 998D. Roman Digits 【打表找规律】
<题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...
- Vue-Router 学习笔记
1:当router-link组件默认渲染成一个a标签,通过to属性指定目标地址,当对应的路由匹配成功,会自动给激活的标签设置class属性值 API 1:to:表示目标路由的链接,当被点击时,内部会立 ...
- 使用CCS调试基于AM335X的SPL、Uboot(原创)
使用CCS调试基于AM335X的SPL.Uboot 一.开发环境 1.硬件平台:创龙AM3359核心板 2.SDK版本:ti-processor-sdk-linux-am335x-evm-03.00. ...
- Farewell Party-构造
Farewell Party 思路 : 转换思路 ,有 a [ i ] 个不相等的 ,那么至少得有 n - a [ i ]个与它相等的. 但是有可能与它拥有相同数目的有很多. 但是为了能够最终 分配成 ...
- 练习七 Procedure中使用DDL
1 在存储过程中使用ddl语句有如下异常: create or replace procedure test_create_table (Table_Name in VARCHAR2, column_ ...