Data type conversion in MongoDB
[问题]
I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of documents created per day and sort by the number asynchronously. The output format is required to be [{_id:'yyyy-MM-dd', cnt:x}]. I tried to use aggregation framework as below. db.Document.aggregate( , {$project: {_id:1, Year:{$year:'$CreationDate'}, Month:{$month:'$CreationDate'}, Date:{$dayOfMonth:'$CreationDate'}}} , {$group: {_id:{$concat:['$Year', '-', '$Month', '-', '$Date']}, cnt:{$sum:1}}} , {$sort:{'cnt':-1}} ); The code gives me error as below: $concat only supports strings, not NumberInt32 I understand this is because $year, $month and $dayOfMonth all return number. It's possible to compose the _id field as an object and re-format it in the desired format in application level. But from technical perspective, I have two questions:
Thanks in advance for any advice. |
[回答]
You can do this with db.Document.aggregate([ { "$group": { "_id":{ "$concat": [ { "$substr": [ { "$year": "$CreationDate" }, 0, 4 ] }, "-", { "$cond": [ { "$gt": [ { "$month": "$CreationDate" }, 9 ] }, { "$substr": [ { "$month": "$CreationDate" }, 0, 2 ] }, { "$concat": [ "0", { "$substr": [ { "$month": "$CreationDate" }, 0, 1 ] }, ]}, ]}, "-", { "$cond": [ { "$gt": [ { "$dayOfMonth": "$CreationDate" }, 9 ] }, { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 2 ] }, { "$concat": [ "0", { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 1 ] }, ]} ]} ] }, { "cnt": { "$sum": 1 } } }} { "$sort":{ "cnt" :-1 }} ]); Possibly better is to just use date math instead, this returns an epoch timestamp value, but it is easy to work into a date object in post processing: db.Document.aggregate([ { "$group": { "_id": { "$subtract": [ { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] }, { "$mod": [ { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] }, 1000 * 60 * 60 * 24 ]} ] }, "cnt": { "$sum": 1 } }}, { "$sort": { "cnt": -1 } } ]) |
来自: https://stackoverflow.com/questions/25176855/data-type-conversion-in-mongodb
Data type conversion in MongoDB的更多相关文章
- java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int
类: Integer id; 反射时: Field f = User.class.getDeclaredField("id"); f.setAccessible(true); in ...
- 【你吐吧c#每日学习】11.10 C# Data Type conversion
implicit explicit float f=12123456.213F int a = Convert.ToInt32(f); //throw exception or int a = (in ...
- PythonStudy——数据类型转化 Data type conversion
类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5') | fl ...
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)
题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)
接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...
- Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion
本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...
- SQL Server发布订阅报错:The conversion of a datetime data type to smalldatetime data type resulted in an out of range value.
执行SQL Server发布订阅时,报错如下信息: The conversion of a datetime data type to smalldatetime data type resulted ...
- JavaScript Type Conversion
Data Types 5 Data Types string, number, boolean, object, function 3 Object Types object, array, date ...
随机推荐
- P1434 [SHOI2002]滑雪 dfs
题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...
- 动态规划状态压缩-poj1143
题目链接:http://poj.org/problem?id=1143 题目描述: 代码实现: #include <iostream> #include <string.h> ...
- bootstrap-fileinput文件上传控件的亲身实践
经理让我帮服务器开发人员开发一个上传文件功能界面,我就想着以前使用过bootstrap-fileinput插件进行文件上传,很不错.赶紧就撸起来了. 1.下载压缩包.插件地址https://githu ...
- kafka-manager配置和使用
kafka-manager配置 最主要配置就是用于kafka管理器状态的zookeeper主机.这可以在conf目录中的application.conf文件中找到. kafka-manager.zkh ...
- visual studio 加入zen-codding
大家都知道zen codding的强大之处大家都知道了,那如何让visual studio也支持呢,直接下载插件安装即可: 插件下载地址:zen-codding for visual studio下载 ...
- Xamarin Essentials教程剪贴板Clipboard
Xamarin Essentials教程剪贴板Clipboard 现在手机设备操作以触屏为主,不便于文本输入.虽然可以通过复制/粘贴的方式,借助系统剪贴板简化操作,但仍然不够方便.如果通过代码操作 ...
- Cisco 12系列 AP 初始化配置-2-初始化配置
用于SSH登陆这个设备 bridge irb bridge 1 route ip 开启路由功能 interface BVI1 ip address 10.15.106.44 255.255.255. ...
- 【DWM1000】 code 解密1一 去掉Main 函数多余内容
蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: 室内定位兴起,DWM1000 作为超宽带UWB的代表,在国内用的越来越多,但是可见资料非常少. 一方面 ...
- mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用
await model.photo.update({ _id: { $in: photoIdsParam } }, { $pull: { customerIds: { code: custCode, ...
- 使用纯CSS制作展开合并立方体特效
显示效果 源码 <html> <head> <meta http-equiv="Content-Type" content="text/ht ...