View a markdown version of this page

$tsIncrement - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$tsIncrement

버전 8.0.1에서 새로 추가되었습니다.

Amazon DocumentDB의 $tsIncrement 연산자는 타임스탬프 값에서 증가하는 서수를 긴 정수로 반환합니다.

파라미터

  • expression: 타임스탬프로 확인되는 표현식입니다.

예제(MongoDB 쉘)

다음 예제에서는 $tsIncrement 연산자를 사용하여 타임스탬프 값에서 서수 증분을 추출하는 방법을 보여줍니다.

샘플 문서 생성

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

쿼리 예제

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

출력

[ {_id: 1, increment: Long("1")}, {_id: 2, increment: Long("2")}, {_id: 3, increment: Long("1")} ]

코드 예제

$tsIncrement 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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()