View a markdown version of this page

$tsSecond - Amazon DocumentDB

$tsSecond

New from version 8.0.1.

The $tsSecond operator in Amazon DocumentDB returns the seconds portion of a Timestamp value as a long integer (the Unix epoch seconds).

Parameters

  • expression: An expression that resolves to a Timestamp.

Example (MongoDB Shell)

The following example shows how to use the $tsSecond operator to extract the seconds portion from Timestamp values.

Create sample documents

db.events.insertMany([ {_id: 1, ts: Timestamp(1678900000, 1)}, {_id: 2, ts: Timestamp(1678900000, 2)}, {_id: 3, ts: Timestamp(1678900001, 1)} ]);

Query example

db.events.aggregate([ { $project: { seconds: { $tsSecond: "$ts" } } } ]);

Output

[ {_id: 1, seconds: Long("1678900000")}, {_id: 2, seconds: Long("1678900000")}, {_id: 3, seconds: Long("1678900001")} ]

Code examples

To view a code example for using the $tsSecond operator, choose the tab for the language that you want to use:

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