UUID()

On this page本页内容

Definition定义

Generates a BSON UUID object.生成BSON UUID对象。

UUID() has the following syntax:语法如下所示:

UUID(<string>)
Parameter参数Type类型Description描述
hex string

Optional.可选。Specify a 36 character string to convert to a UUID BSON object. 指定要转换为UUID BSON对象的36个字符的字符串。If not provided, MongoDB generates a random UUID in RFC 4122 v4 format.如果未提供,MongoDB将生成RFC 4122 v4格式的随机UUID。

Changed in version 3.6.在版本3.6中更改。In earlier versions of the mongo shell, UUID required a hexadecimal string argument. 在早期版本的mongo shell中,UUID需要一个十六进制字符串参数。See the 3.4 manual.请参阅3.4手册

returns:A BSON UUID object.

Example示例

Convert Character String to UUID将字符串转换为UUID

Create a 36 character string you wish to convert to a UUID:创建要转换为UUID的36个字符的字符串:

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

The following command outputs the myuuid variable as a BSON UUID object:以下命令将myuuid变量作为BSON UUID对象输出:

UUID(myuuid)

This command generates the following output:此命令生成以下输出:

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

Generate Random UUID生成随机UUID

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

UUID()

This command outputs a random UUID in the following form:此命令以以下形式输出随机UUID:

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

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