Definition定义
Creates an ObjectId from a hexadecimal value.从十六进制值创建ObjectId。
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.createFromHexString( <hexadecimalString> )
The hexadecimalString field specifies a string that contains a 24 character hexadecimal value. For example, "64c13ab08edf48a008793cac".hexadecimalString字段指定了一个包含24个字符的十六进制值的字符串。例如,"64c13ab08edf48a008793cac"。
Examples示例
The following examples show how to add an object identifier to a document using 以下示例显示了如何使用ObjectId.createFromHexString() and how the object identifier appears in the output when retrieved.ObjectId.createFromHexString()将对象标识符添加到文档中,以及检索时对象标识符如何显示在输出中。
Create Collection Containing Document with Object Identifier创建包含对象标识符的文档的集合
The following example creates a collection named 以下示例创建了一个名为objectIdentifierValuesFromHex:objectIdentifierValuesFromHex的集合:
db.objectIdentifierValuesFromHex.insertOne( {
_id: 0,
objectIdentifierValue: ObjectId.createFromHexString( "64c13ab08edf48a008793cac" )
} )
The objectIdentifierValue field contains the object identifier created from the hexadecimal string specified in ObjectId.createFromHexString().objectIdentifierValue字段包含根据ObjectId.createFromHexString()中指定的十六进制字符串创建的对象标识符。
Retrieve Document from Collection with Object Identifier使用对象标识符从集合中检索文档
The following example retrieves the document:以下示例检索文档:
db.objectIdentifierValuesFromHex.findOne( { _id: 0 } )
Example output:示例输出:
{
_id: 0,
objectIdentifierValue: ObjectId("64c13ab08edf48a008793cac")
}