$millisecond
The $millisecond operator in Amazon DocumentDB is used to extract the millisecond portion of a date value.
Parameters
None
Example (MongoDB Shell)
This example demonstrates how to use the $millisecond operator to extract the millisecond portion of a date value.
Create sample documents
db.events.insert([ { "name": "Event 1", "timestamp": ISODate("2023-04-21T10:30:15.123Z") }, { "name": "Event 2", "timestamp": ISODate("2023-04-21T10:30:15.456Z") }, { "name": "Event 3", "timestamp": ISODate("2023-04-21T10:30:15.789Z") } ])
Query example
db.events.aggregate([ { $project: { name: 1, milliseconds: { $millisecond: "$timestamp" } } } ])
Output
[
{
"_id": ObjectId("644332a42054ed1b0d15f0c1"),
"name": "Event 1",
"milliseconds": 123
},
{
"_id": ObjectId("644332a42054ed1b0d15f0c2"),
"name": "Event 2",
"milliseconds": 456
},
{
"_id": ObjectId("644332a42054ed1b0d15f0c3"),
"name": "Event 3",
"milliseconds": 789
}
]
Code examples
To view a code example for using the $millisecond command, choose the tab for the language that you want to use: