$millisecond - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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:

Node.js
const { MongoClient } = require('mongodb'); async function main() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const events = db.collection('events'); const result = await events.aggregate([ { $project: { name: 1, milliseconds: { $millisecond: '$timestamp' } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] events = db['events'] result = list(events.aggregate([ { '$project': { 'name': 1, 'milliseconds': { '$millisecond': '$timestamp' } } } ])) print(result) client.close()