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 ...
随机推荐
- Vue全局API总结
1.extend用于创建一个子类Vue,用$mount来挂载 <body> <div id="app"></div> <script> ...
- Codechef STMINCUT S-T Mincut (CodeChef May Challenge 2018) kruskal
原文链接http://www.cnblogs.com/zhouzhendong/p/9010945.html 题目传送门 - Codechef STMINCUT 题意 在一个有边权的无向图中,我们定义 ...
- springboot配置log4j
maven 配置jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- word,excel,ppt在线预览功能
我们在开发web项目时,尤其类似oa功能时总会遇到上传附件并在线预览的功能,发现一款api比较好使,下面简单介绍一下. 微软官网本身提供了在线预览的API 首先将要预览的文档转成.docx,.xlsx ...
- SVM:SVM之Classification根据已有大量数据集案例,输入已有病例的特征向量实现乳腺癌诊断高准确率预测—Jason niu
load BreastTissue_data.mat n = randperm(size(matrix,1)); train_matrix = matrix(n(1:80),:); train_lab ...
- 1402 后缀数组 (hash+二分)
描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围.在本题中,我们希望使用快排.Hash与二分实现一个简单的 O(n log^2n ) 的后缀数组 ...
- GYM 101755 K.Video Reviews 【贪心】+【二分】
<题目链接> 题目大意: 一家公司想让n个人给他们的产品评论,所以依次去找这n个人,第i个人会评论当且仅当已经有ai个人评论或他确实对这个产品感兴趣,但是这n个人都不对这个产品感兴趣,问这 ...
- ctf study of jarvisoj reverse
[61dctf] androideasy 164求解器 50 相反 脚本如下: s='' a=113, 123, 118, 112, 108, 94, 99, 72, 38, 68, 72, 87, ...
- 使用Log4j日志处理
Springboot日志默认使用的是logback,本文将介绍将springboot项目日志修改为log4j. 首先要将默认的日志依赖排除,然后引用log4j,pom文件代码如下: <?xml ...
- C#调用WebService的简单方式
WebServiceCallpublic class WebServiceCall { public void Call() { string url = "http://localhost ...