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 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...
随机推荐
- sqlldr的用法 (这个最完整)
转自:http://blog.chinaunix.net/uid-23622436-id-2394093.html 一:在 Oracle 数据库中,我们通常在不同数据库的表间记录进行复制或迁移时会用以 ...
- Python编程基础[函数和面向对象](三)
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...
- css图片根据div宽高比例自适应
1.div布局 <div class="card-img-show"> <div class="upload-img-conss" > ...
- Alpha(10/10)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- 向量图兼容组件VectorCompat
向量图兼容组件VectorCompat Android从5.0(API Level 21)开始,支持矢量图和动画矢量图.采用这两种图,可以避免传统图片因为缩放而产生失真.VectorCompat组件是 ...
- Redis自学笔记:3.5入门-集合类型
3.5集合类型 3.5.1介绍 在集合中的每个元素都是不同的,且没有顺序 表3-4集合类型和列表类型的对比 - 集合类型 列表类型 存储内容 至多232-1个字符串 至多232-1个字符串 有序性 否 ...
- 3.27模拟赛 sutoringu(后缀数组)
\(\color{white}{mjt是机房模拟赛独自切过题的唯一的人...}\) (应本人要求删掉惹) \(Description\) 给你\(n,k\)和长为\(n\)的字符串\(s\).一个区间 ...
- Some Conclusions.
目录 DP 四边形不等式 数论 & 数学 数据结构 树链剖分 左偏树的性质及\(O(n)\)的构造 图论 树 二分图 竞赛图 平面图 双连通分量 字符串 后缀自动机 复杂度分析 没什么好写的. ...
- Scrapy基础(七)————图片的简单下载
scrapy 提供了自动下载图片到本地的功能,通过项目管道设置 一: 在setting 文件中ITEM_PIPELINE添加: 'scrapy.pipelines.images.ImagesPipel ...
- [ONTAK2015]Tasowanie
[ONTAK2015]Tasowanie 题目大意: 给你两个长度分别为\(n(n\le2\times10^5)\)的序列\(A,B\),将\(A,B\)进行二路归并,使得最后得到的序列字典序最小.求 ...