在MongoDB中将字符串转换为ISODate [英] Convert string to ISODate in MongoDB

查看:56
本文介绍了在MongoDB中将字符串转换为ISODate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB的新手,但我陷入了String to Date的转换.在数据库中,日期项以String类型存储为"date":" 2015-06-16T17:50:30.081Z"

I am new to MongoDB and I am stuck on the String to Date conversion. In the db the date item is stored in String type as "date":"2015-06-16T17:50:30.081Z"

我想按日期对文档进行分组并计算每天的总和,因此我必须从日期字符串中提取年,月和日,并擦除小时,分钟和秒.我尝试了多种方法,但是它们要么返回1970-01-01的日期类型,要么返回当前日期.

I want to group the docs by date and calculate the sum of each day so I have to extract year, month and day from the date string and wipe off the hrs, mins and seconds. I have tried multiple way but they either return a date type of 1970-01-01 or the current date.

此外,我想将以下mongo查询转换为python代码,这使我遇到相同的问题,我无法在python中调用javascript函数,并且 datetime 无法解析mongo语法 $ date 之一.

Moreover, I want to convert the following mongo query into python code, which get me the same problem, I am not able to call a javascript function in python, and the datetime can not parse the mongo syntax $date either.

我尝试过:

new Date("2015-06-16T17:50:30.081Z")
new Date(Date.parse("2015-06-16T17:50:30.081Z"))
etc...

我可以很完美地发现字符串是用Java脚本还是Python给出的,我知道有多种解析它的方法.但是我不知道如何在MongoDB查询中做到这一点.

I am perfectly find if the string is given in Javascript or in Python, I know more than one way to parse it. However I have no idea about how to do it in MongoDB query.

  db.collection.aggregate([
                    {
                        //somthing
                    },
                    { 
                        '$group':{
                            '_id':{ 
                                'month':  (new Date(Date.parse('$approTime'))).getMonth(), 
                                'day': (new Date(Date.parse('$approTime'))).getDate(), 
                                'year':  (new Date(Date.parse('$approTime'))).getFullYear(),
                                'countries':'$countries'
                            },
                             'count': {'$sum':1}

                    }
                }
])

推荐答案

如果您可以确定输入日期字符串的格式,而您只是想获取唯一的YYYYMMDD的计数,则只需$ project子字符串,然后分组:

If you can be assured of the format of the input date string AND you are just trying to get a count of unique YYYYMMDD, then just $project the substring and group on it:

var data = [
        { "name": "buzz", "d1": "2015-06-16T17:50:30.081Z"},
        { "name": "matt", "d1": "2018-06-16T17:50:30.081Z"},
        { "name": "bob", "d1": "2018-06-16T17:50:30.081Z"},
        { "name": "corn", "d1": "2019-06-16T17:50:30.081Z"},
      ];

db.foo.drop();
db.foo.insert(data);

db.foo.aggregate([
     { "$project": {
         "xd": { "$substr": [ "$d1", 0, 10 ]}
         }
 },
 { "$group": {
             "_id": "$xd",
             "n": {$sum: 1}
         }
     }
              ]);

{ "_id" : "2019-06-16", "n" : 1 }
{ "_id" : "2018-06-16", "n" : 2 }
{ "_id" : "2015-06-16", "n" : 1 }

这篇关于在MongoDB中将字符串转换为ISODate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆