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 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...
随机推荐
- selenium课程笔记
selenium课程笔记第一天(2017-7-1) 一.配置火狐浏览器 运行:firefox.exe -p -no -remote selenium课程笔记第二天 用Eclipse+java+sele ...
- Vue全局API总结
1.extend用于创建一个子类Vue,用$mount来挂载 <body> <div id="app"></div> <script> ...
- PAT (Basic Level) Practise - 我要通过!
“答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...
- P1967 货车运输
P1967 货车运输最大生成树+lca+并查集 #include<iostream> #include<cstdio> #include<queue> #inclu ...
- XamarinEssentials教程首选项Preferences判断项目是否存在
XamarinEssentials教程首选项Preferences判断项目是否存在 首选项也可以称为偏好设置.它以键值对的形式保存数据,适用于用户少量数据的存储.例如,将用户的个性化设置保存后,每次打 ...
- WPFToolkit DataGrid 使用介绍zz
首先:这版本需要.NetFrameWork SP1的支持 WPF Toolkit - February 2010 Release 下载地址:http://wpf.codeplex.com/rele ...
- Shell脚本笔记(九)数组
数组 一)定义 #最常用:小括号加空格: a=( ) #键值对形式: b=([]=x []=men []=z) #定义变量的形式 c[]=; c[]=; c[]= #命令结果作为数组元素 d=($(c ...
- 【开源GPS追踪】 之 手机端安卓版
GPS追踪,后台是利用的是开源的Opengts,可以通过web方式浏览位置信息.这里介绍一款手机端软件go Tracker. 这款软件是在Google app 上找到的,目前还没有找到源码,用了几天有 ...
- [USACO08OPEN]寻宝之路Clear And Present Danger
OJ题号:洛谷2910 思路:Floyd #include<cstdio> #include<algorithm> using namespace std; int main( ...
- js实现一个一个打印字体的功能
var str = "ddll台湾八百壮士抗议苹果正式发邀请函西安铁警查倒票案自制航模逼停高铁林志玲遭老总熊抱拖拽游艇事故通报大马外交官被暗杀鹿晗又和邮筒合影奥迪男辱骂环卫工 " ...