Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to ISODate in MongoDB

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"

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.

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.

I have tried:

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

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}

                    }
                }
])
like image 530
pledez Avatar asked Jun 16 '15 16:06

pledez


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

What is ISODate in MongoDB?

ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.

Does MongoDB convert date to UTC?

MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.


2 Answers

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 }
like image 92
Buzz Moschetti Avatar answered Sep 28 '22 13:09

Buzz Moschetti


Starting in Mongo 4.0, you can use "$toDate" to convert a string to a date:

// { 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.collection.aggregate(
  { $group: {
    _id: { $dateToString: { date: { $toDate: "$d1" }, format: "%Y-%m-%d" } },
    n: { $sum: 1 }
  }}
)
// { _id: "2015-06-16", n: 1 }
// { _id: "2018-06-16", n: 2 }
// { _id: "2019-06-16", n: 1 }

Within the group stage, this:

  • first converts strings (such as "2015-06-16T17:50:30.081Z") to date objects (ISODate("2015-06-16T17:50:30.081Z")) using the "$toDate" operator.

  • then converts the converted date (such as ISODate("2015-06-16T17:50:30.081Z")) back to string ("2015-06-16") but this time with this format "%Y-%m-%d", using the $dateToString operator.

like image 44
Xavier Guihot Avatar answered Sep 28 '22 13:09

Xavier Guihot