View a markdown version of this page

$toUUID - Amazon DocumentDB

$toUUID

New from version 8.0.1.

The $toUUID operator in Amazon DocumentDB converts a string value to a UUID (Binary subtype 4).

Parameters

  • expression: An expression that resolves to a string in UUID format (for example, "12345678-1234-1234-1234-123456789abc").

Example (MongoDB Shell)

The following example shows how to use the $toUUID operator to convert string values to UUID format.

Create sample documents

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

Query example

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

Output

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

Code examples

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

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