Database Manual / Reference / mongosh Methods / Object Constructors

ObjectId.createFromBase64() (mongosh method方法)

Definition定义

Creates an ObjectId from a base64 value.

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的源代码可用、免费使用和自我管理版本

Syntax语法

ObjectId.createFromBase64( <base64String> [ , <subType> ] )

Method Fields方法字段

The method accepts the following fields:该方法接受以下字段:

Field字段Type类型Description描述
base64StringString字符串Specifies a 16 character base64 value. For example, "SGVsbG8gV29ybGQh".指定一个16个字符的base64值。例如,"SGVsbG8gV29ybGQh"
subTypeInteger整数

Optional. 可选。Specifies a binary subtype.指定二进制子类型。

ValueDescription描述
0Generic binary subtype通用二进制子类型
1Function
2Byte array (deprecated)
3Old UUID (deprecated)
4UUID
5MD5
128 through 255User defined用户自定义

Examples示例

The following examples show how to add an object identifier to a document using ObjectId.createFromBase64() and how the object identifier appears in the output when retrieved.以下示例显示了如何使用ObjectId.createFromBase64()将对象标识符添加到文档中,以及检索时对象标识符如何显示在输出中。

Create Collection Containing Document with Base64 Number创建包含Base64数字的文档的集合

The following example creates a collection named objectIdentifierValuesFromBase64:以下示例创建了一个名为objectIdentifierValuesFromBase64的集合:

db.objectIdentifierValuesFromBase64.insertOne( {
_id: 0,
objectIdentifierValue: ObjectId.createFromBase64( "SGVsbG8gV29ybGQh" )
} )

The objectIdentifierValue field contains the object identifier created from the base64 string specified in ObjectId.createFromBase64().objectIdentifierValue字段包含根据ObjectId.createFromBase64()中指定的base64字符串创建的对象标识符。

Retrieve Document from Collection with Object Identifier使用对象标识符从集合中检索文档

The following example retrieves the document:以下示例检索文档:

db.objectIdentifierValuesFromBase64.findOne( { _id: 0 } )

Example output:示例输出:

{
_id: 0,
objectIdentifierValue: ObjectId("48656c6c6f20576f726c6421")
}