Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb : Why convert string date to ISOdate if comparison operators work?

I have the following kind of document:

{ 
  "_id" : ObjectId("538d64a11ca6e50941fda4d9"), "_id" : "538d518e20b8fd642e0000e8", 
  "posts" : "some stuff", "date" : "2014-06-02"
}

Using comparison operators for a string date (Not the Mongodb ISODate) works:

> collection.find({"date": {"$gte": "2014-06-02"}})

So why shall we (bother to) convert string dates to an ISODate then?

like image 593
Antoine Brunel Avatar asked Sep 05 '14 13:09

Antoine Brunel


1 Answers

Probably the biggest advantage of using the MongoDB BSON Date type instead of a string is that you can only use the aggregate Date operators with BSON Date values.

But if you don't need to do any aggregation of your data, using a sortable string format to represent dates is fine and often cleaner as you don't need to deal with time zone related display problems.

like image 157
JohnnyHK Avatar answered Sep 22 '22 06:09

JohnnyHK