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).
$tsIncrement
New from version 8.0.1.
The $tsIncrement operator in Amazon DocumentDB returns the incrementing ordinal from a Timestamp value as a long integer.
Parameters
Example (MongoDB Shell)
The following example shows how to use the $tsIncrement operator to extract the ordinal increment 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: { increment: { $tsIncrement: "$ts" } } }
]);
Output
[
{_id: 1, increment: Long("1")},
{_id: 2, increment: Long("2")},
{_id: 3, increment: Long("1")}
]
Code examples
To view a code example for using the $tsIncrement 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: { increment: { $tsIncrement: "$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': {'increment': {'$tsIncrement': '$ts'}}}
]))
print(result)
finally:
client.close()
example()