This article presents 5 ways to return the seconds 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 seconds portion from the born
field of those documents.
The $second
Operator
The $second
operator is designed specifically to return a document with the seconds portion of a given date.
We can run the following code to return the seconds portion from the born
field in the above document.
db.cats.aggregate(
[
{
$project:
{
_id: 0,
birthSecond: { $second: "$born" }
}
}
]
)
Result:
{ "birthSecond" : 15 } { "birthSecond" : 20 } { "birthSecond" : 1 }
You can also specify the timezone when using the $second
operator.
See MongoDB $second
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 seconds portion is returned if required.
There are format specifiers for each date part. The %S
format specifier returns the second portion (2 digits, zero padded).
Example:
db.cats.aggregate(
[
{
$project: {
_id: 0,
birthSecond: { $dateToString: { format: "%S", date: "$born" } }
}
}
]
)
Result:
{ "birthSecond" : "15" } { "birthSecond" : "20" } { "birthSecond" : "01" }
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 second
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 second
field in the next stage.
Here’s what happens if we add another projection for just the second
field:
db.cats.aggregate(
[
{
$project:
{
_id: 0,
dateParts: { $dateToParts: { date: "$born" } }
}
},
{
$project:
{
birthSecond: "$dateParts.second"
}
}
]
)
Result:
{ "birthSecond" : 15 } { "birthSecond" : 20 } { "birthSecond" : 1 }
So, whenever you use $dateToParts
in your pipeline, you will have access to the second
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 getSeconds()
or getUTCSeconds()
to return just the seconds.
db.cats.find().forEach(
function(c) {
print(
c.born.getUTCSeconds()
);
}
);
Result:
15 20 1
The getUTCSeconds()
JavaScript method returns an integer number, between 0 and 59, representing the seconds in the given date according to universal time.
The getSeconds()
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 seconds 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.getUTCSeconds();
return c;
}
);
Result:
[ 15, 20, 1 ]