ObjectId.createFromHexString()
On this page
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of MongoDB server.
Definition
Creates an ObjectId from a hexadecimal value.
Syntax
The hexadecimalString
field specifies a string that contains a 24
character hexadecimal value. For example,
"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.
Create Collection Containing Document with Object Identifier
The following example creates a collection named
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()
.
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") }