翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$tsIncrement
バージョン 8.0.1 から新規。
Amazon DocumentDB の $tsIncrement演算子は、タイムスタンプ値から増分序数を長い整数として返します。
パラメータ
例 (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()