Database Manual / Reference / mongosh Methods / Object Constructors

UUID() (mongosh method方法)

Definition定义

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

UUID() has the following syntax:具有以下语法:

UUID(<string>)
Parameter参数Type类型Description描述
hexstring字符串Optional. 可选。Specify a 36 character string to convert to a UUID BSON object. 指定一个36个字符的字符串以转换为UUID BSON对象。If not provided, MongoDB generates a random UUID in RFC 4122 v4 format.如果没有提供,MongoDB将生成RFC 4122 v4格式的随机UUID。
Returns:返回A BSON UUID object.BSON UUID对象。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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()方法来生成随机UUID:

UUID()

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

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