ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

$createUUID

The $createUUID expression takes no arguments and returns a UUID BSON type (i.e. a BinData with subtype=4). The UUID is a version 4 UUID conforming to RFC 4122.

$createUUID

A $createUUID expression takes the form of an empty object {}.

The $createUUID expression is an empty object {} and has no internal syntax.

Pass $createUUID: {} in your aggregation pipeline to generate a UUID value.

The following examples demonstrate a basic use of the $createUUID expression.

{$project: {
a: {$createUUID: {}}
b: {$createUUID: {}}
}}
{
a: UUID('e14ac078-b627-48e8-b009-77dbd7150e75')
b: UUID(‘f18689fe-6dfc-4823-a01e-32792f0091fe’)
}

You can use a $toString expression to convert the output of your $createUUID expression to a string value.

{$project: {
a: {$toString: {$createUUID: {}}}
}}
{
a: 'e14ac078-b627-48e8-b009-77dbd7150e75'
}
{$project: {
a: {$createUUID: {“foo”: “bar”}}
}}
Error– $createUUID does not accept arguments.