This article presents 5 ways to return the milliseconds portion from a Date object in MongoDB.
Sample Data
Suppose we have a collection called cats
with the following documents:
{ "_id" : 1, "name" : "Scratch", "born" : ISODate("2021-01-03T23:30:15.123Z") } { "_id" : 2, "name" : "Meow", "born" : ISODate("2019-12-08T04:00:20.112Z") } { "_id" : 3, "name" : "Fluffy", "born" : ISODate("2020-09-24T10:45:01.007Z") }
The following examples demonstrate various options for returning the milliseconds portion from the born
field of those documents.
The $millisecond
Operator
The $millisecond
operator is designed specifically to return a document with the milliseconds portion of a given date.
We can run the following code to return the milliseconds portion from the born
field in the above document.
db.cats.aggregate(
[
{
$project:
{
_id: 0,
birthMillisecond: { $millisecond: "$born" }
}
}
]
)
Result:
{ "birthMillisecond" : 123 } { "birthMillisecond" : 112 } { "birthMillisecond" : 7 }
The $millisecond
operator also accepts a timezone
argument.
See MongoDB $millisecond
for more information and examples.
The $dateToString
Operator
The $dateToString
operator converts a date object to a string according to a user-specified format. The user can therefore specify that just the milliseconds portion is returned if required.
There are format specifiers for each date part. The %L
format specifier returns the millisecond portion (3 digits, zero padded).
Example:
db.cats.aggregate(
[
{
$project: {
_id: 0,
birthMillisecond: { $dateToString: { format: "%L", date: "$born" } }
}
}
]
)
Result:
{ "birthMillisecond" : "123" } { "birthMillisecond" : "112" } { "birthMillisecond" : "007" }
See MongoDB $dateToString
for more information and examples.
The $dateToParts
Operator
The $dateToParts
operator returns a document that contains the constituent parts of a given BSON Date value as individual properties. The properties returned are year
, month
, day
, hour
, minute
, second
and millisecond
.
We could therefore use $dateToParts
in one pipeline stage, then add another pipeline stage that extracts the millisecond
part.
Here’s what $dateToParts
returns for our three documents:
db.cats.aggregate(
[
{
$project:
{
_id: 0,
dateParts: { $dateToParts: { date: "$born" } }
}
}
]
).pretty()
Result:
{ "dateParts" : { "year" : 2021, "month" : 1, "day" : 3, "hour" : 23, "minute" : 30, "second" : 15, "millisecond" : 123 } } { "dateParts" : { "year" : 2019, "month" : 12, "day" : 8, "hour" : 4, "minute" : 0, "second" : 20, "millisecond" : 112 } } { "dateParts" : { "year" : 2020, "month" : 9, "day" : 24, "hour" : 10, "minute" : 45, "second" : 1, "millisecond" : 7 } }
This result can then be passed to the next stage in the pipeline, and we can therefore extract just the millisecond
field in the next stage.
Here’s what happens if we add another projection for just the millisecond
field:
db.cats.aggregate(
[
{
$project:
{
_id: 0,
dateParts: { $dateToParts: { date: "$born" } }
}
},
{
$project:
{
birthMillisecond: "$dateParts.millisecond"
}
}
]
)
Result:
{ "birthMillisecond" : 123 } { "birthMillisecond" : 112 } { "birthMillisecond" : 7 }
So, whenever you use $dateToParts
in your pipeline, you will have access to the millisecond
field (and the other date parts) in the next stage.
See MongoDB $dateToParts
for more information and examples.
The forEach()
Method
You can use cursor.forEach()
to iterate through the cursor, using a JavaScript method such as getMilliseconds()
or getUTCMilliseconds()
to return just the milliseconds.
db.cats.find().forEach(
function(c) {
print(
c.born.getUTCMilliseconds()
);
}
);
Result:
123 112 7
The getUTCSeconds()
JavaScript method returns an integer number, between 0 and 999, representing the milliseconds portion of the given date object.
The getMilliseconds()
method returns it in local time.
You may also notice that the previous options return a whole document that contains a name/value pair, whereas this option returns just the actual milliseconds value, and not the whole document.
The map()
Method
The cursor.map()
method applies a function to each document visited by the cursor and combines the values into an array.
Example:
db.cats.find().map(
function(c) {
c = c.born.getUTCMilliseconds();
return c;
}
);
Result:
[ 123, 112, 7 ]