Docs Menu
Docs Home
/
MongoDB Manual
/ / /

ObjectId.createFromHexString()

On this page

  • Definition
  • Compatibility
  • Syntax
  • Examples

Creates an ObjectId from a hexadecimal value.

This method is available in deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

ObjectId.createFromHexString( <hexadecimalString> )

The hexadecimalString field specifies a string that contains a 24 character hexadecimal value. For example, "64c13ab08edf48a008793cac".

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.

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().

The following example retrieves the document:

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

Example output:

{
_id: 0,
objectIdentifierValue: ObjectId("64c13ab08edf48a008793cac")
}

Back

ObjectId.createFromBase64