Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for Dates in MongoDB running on MongoLab?

Instead of ObjectId('82he921he982he82') its "_id":{"$oid":"82he921he982he82"} for object Ids, but I have no idea how to create a date value in a document on MongoLab :s

like image 965
SomethingOn Avatar asked Nov 04 '11 06:11

SomethingOn


People also ask

How are dates written in MongoDB?

You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats: new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.

What format does MongoDB store dates?

MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.

How are dates stored in MongoDB?

The best format to store date and time in MongoDB is native javascript Date() or ISO date format as it internally converts it into BSON native Date object.

What is date type in MongoDB?

The recommended way to store dates in MongoDB is to use the BSON Date data type. The. BSON Specification. refers to the Date type as the UTC datetime and is a 64-bit integer. It represents the number of milliseconds since the.


2 Answers

If you are using one of the drivers, you create a date via that language's Date class. For instance, in Javascript (like Node.js) it would be 'new Date(11,11,11)'. If using the MongoLab REST API or admin UI you represent dates with this syntax:

  { "$date": "2010-10-28T23:07:11Z" }
like image 68
will Avatar answered Nov 15 '22 06:11

will


Also if an item has:

"dateAdded": {
   "$date": "2012-07-30T15:26:15.321Z"
}

in one of it's entries, then you can find it by searching:

{
    "dateAdded": {
        "$gt": {
            "$date": "2012-07-28T23:07:11Z"
        }
    }
}
like image 35
Justin Harris Avatar answered Nov 15 '22 06:11

Justin Harris