View a markdown version of this page

$toUUID - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$toUUID

バージョン 8.0.1 から新規。

Amazon DocumentDB の $toUUID演算子は、文字列値を UUID (バイナリサブタイプ 4) に変換します。

パラメータ

  • expression: UUID 形式の文字列に解決される式 (例: "12345678-1234-1234-1234-123456789abc")。

例 (MongoDB シェル)

次の例は、 $toUUID演算子を使用して文字列値を UUID 形式に変換する方法を示しています。

サンプルドキュメントを作成する

db.records.insertMany([ {_id: 1, uuidStr: "550e8400-e29b-41d4-a716-446655440000"}, {_id: 2, uuidStr: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"} ]);

クエリの例

db.records.aggregate([ { $project: { uuid: { $toUUID: "$uuidStr" } } } ]);

出力

[ {_id: 1, uuid: UUID("550e8400-e29b-41d4-a716-446655440000")}, {_id: 2, uuid: UUID("6ba7b810-9dad-11d1-80b4-00c04fd430c8")} ]

コードの例

$toUUID 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js
const { MongoClient } = 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('records'); const result = await collection.aggregate([ { $project: { uuid: { $toUUID: "$uuidStr" } } } ]).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['records'] result = list(collection.aggregate([ {'$project': {'uuid': {'$toUUID': '$uuidStr'}}} ])) print(result) finally: client.close() example()