UUID()

Definition

Generates a BSON UUID object.

UUID() has the following syntax:

  1. UUID(<string>)
ParameterTypeDescription
hexstringOptional. Specify a 36 character string to convert to a UUID BSON object. Ifnot provided, MongoDB generates a random UUID inRFC 4122 v4 format.Changed in version 3.6: In earlier versions of the mongo shell,UUID required a hexadecimal string argument. See the3.4 manual.
returns:A BSON UUID object.

Example

Convert Character String to UUID

Create a 36 character string you wish to convert to a UUID:

  1. var myuuid = '3b241101-e2bb-4255-8caf-4136c566a962'

The following command outputs the myuuid variable as a BSON UUID object:

  1. UUID(myuuid)

This command generates the following output:

  1. UUID("3b241101-e2bb-4255-8caf-4136c566a962")

Generate Random UUID

You can run the UUID() method withoutspecifying an argument to generate a random UUID:

  1. UUID()

This command outputs a random UUID in the following form:

  1. UUID("dee11d4e-63c6-4d90-983c-5c9f1e79e96c")

Changed in version 3.6: In earlier versions of the mongo shell,UUID required a hexadecimal string argument.