$second
The $second operator in Amazon DocumentDB extracts the seconds component from a date or timestamp. It is used to retrieve the seconds value from a date or timestamp field.
Parameters
-
expression: The date or timestamp field to extract the seconds value from. This expression can be a field path or any valid expression that resolves to a date or timestamp.
Example (MongoDB Shell)
The following example demonstrates how to use the $second operator to extract the seconds component from a date field.
Create sample documents
db.users.insertMany([ { name: "John", dob: new Date("1990-05-15T12:30:45Z") }, { name: "Jane", dob: new Date("1985-09-20T23:59:59Z") }, { name: "Bob", dob: new Date("2000-01-01T00:00:00Z") } ]);
Query example
db.users.aggregate([{ $project: { name: 1, dobSeconds: { $second: "$dob" } } }])
Output
[
{ "_id" : ObjectId("6089a9c306a829d1f8b456a1"), "name" : "John", "dobSeconds" : 45 },
{ "_id" : ObjectId("6089a9c306a829d1f8b456a2"), "name" : "Jane", "dobSeconds" : 59 },
{ "_id" : ObjectId("6089a9c306a829d1f8b456a3"), "name" : "Bob", "dobSeconds" : 0 }
]
Code examples
To view a code example for using the $second command, choose the tab for the language that you want to use: