In MongoDB, the Date()
method returns a date either as a string or as a Date object.
- When you call it as
Date()
, it returns the current date as a string in themongo
shell. - When you call it as
new Date()
, it returns the current date as a Date object.
You can also provide a specific date as an argument to new Date()
, so that it uses that date.
Example – Date()
Here’s an example of inserting a document into a collection, and using Date()
as the value for one of the fields:
db.dogs.insert(
{
"_id" : 1,
"name" : "Fetch",
"lastModified" : Date()
}
)
After running that code, we can take a look at the resulting document:
db.dogs.find().pretty()
Result:
{ "_id" : 1, "name" : "Fetch", "lastModified" : "Wed Jan 27 2021 10:15:53 GMT+1000 (AEST)" }
We can see that the lastModified
date field uses a date string as its value.
Example – new Date()
As mentioned, if you want to return a date object instead of a date string, use new Date()
.
Example:
db.dogs.insert(
{
"_id" : 2,
"name" : "Wag",
"lastModified" : new Date()
}
)
Simply prepending Date()
with new
results in a Date object rather than a date string.
Now let’s check the collection of documents again:
db.dogs.find().pretty()
Result:
{
"_id" : 1,
"name" : "Fetch",
"lastModified" : "Wed Jan 27 2021 10:15:53 GMT+1000 (AEST)"
}
{
"_id" : 2,
"name" : "Wag",
"lastModified" : ISODate("2021-01-27T00:19:08.862Z")
}
We can see that the date in the second document has been wrapped with the ISODate()
helper. The ISODate is in UTC.
Provide a Specific Date
You can provide your own date when calling the Date()
method.
To do this, pass an ISO-8601 date string with a year within the inclusive range 0
through 9999
to the new Date()
constructor or the ISODate()
function.
Here’s an example.
db.dogs.update(
{ "_id" : 1 },
{ $set : { "born" : new Date( "2020-10-07" ) } }
)
Now let’s check the document:
db.dogs.find( { "_id": 1 } ).pretty()
Result:
{ "_id" : 1, "name" : "Fetch", "lastModified" : "Wed Jan 27 2021 10:15:53 GMT+1000 (AEST)", "born" : ISODate("2020-10-07T00:00:00Z") }
We can see that the born field has been added, and it has been set to the date provided.
You can provide the date in the following formats.
Format | Description |
---|---|
YYYY-mm-dd | Returns the ISODate with the specified date. |
YYYY-mm-ddTHH:MM:ss | Specifies the datetime in the client’s local timezone and returns the ISODate with the specified datetime in UTC. |
YYYY-mm-ddTHH:MM:ssZ | Specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC. |
Integer | You can also provide an integer as the value. When you do this, it specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance. |
Unix Epoch
Here’s an example of specifying an integer as the argument to new Date()
.
Doing this specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate
instance.
db.dogs.update(
{ "_id" : 2 },
{ $set : { "born" : new Date( 1601207768012 ) } }
)
Now let’s check the document:
db.dogs.find( { "_id": 2 } ).pretty()
Result:
{ "_id" : 2, "name" : "Wag", "lastModified" : ISODate("2021-01-27T00:19:08.862Z"), "born" : ISODate("2020-09-27T11:56:08.012Z") }