Docs Menu

Docs HomeMongoDB Manual

ObjectId.createFromHexString()

On this page

  • Definition
  • Syntax
  • Examples

Creates an ObjectId from a hexadecimal value.

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")
}
←  ObjectId.createFromBase64()ObjectId.getTimestamp() →