The Wayback Machine - https://web.archive.org/web/20260228025418/https://www.geeksforgeeks.org/mongodb/what-is-objectid-in-mongodb/

ObjectId in MongoDB

Last Updated : 10 Feb, 2026

The _id field uniquely identifies each MongoDB document, using ObjectId by default to ensure uniqueness and efficient data management.

  • Every document contains a unique _id field.
  • Acts as the primary key for documents in a collection.
  • Default _id type is ObjectId.
  • ObjectId is a 12-byte BSON data type.
  • Ensures uniqueness across machines and processes.
  • Embeds metadata such as the creation timestamp.

ObjectId Structure

The ObjectId is composed of 12 bytes, broken down as follows:

  • The ObjectId is 12 bytes in total.
  • The first 4 bytes store the Unix timestamp.
  • The next 3 bytes store the machine ID on which the MongoDB server is running.
  • The next 2 bytes store the process ID.
  • The last 3 bytes are used as a counter for uniqueness.
objectid_structure

The ObjectId is represented as a hexadecimal string, and its default format is:

ObjectId(<hexadecimal>)

ObjectId accepts one parameter which is optional Hexadecimal ObjectId in String. We can give our own ObjectId to the document but it must be unique.

*db.<collectionname>.insertOne({"_id":"231231"})

Example of ObjectId in MongoDB

When we insert a document into a collection, MongoDB automatically assigns a unique_id using the ObjectId format.

  • Database : gfg
  • Collection: student_gfg
Screenshot-2026-02-10-125059
  • MongoDB automatically generates a unique _id for the document.
  • The document is inserted into the student_gfg collection.
  • The _id field is assigned an ObjectId value by default.

Methods of MongoDB ObjectId

MongoDB provides several useful methods to interact with the ObjectId. These methods allow you to access the timestamp, convert the ObjectId to a string, and more.

  • str: Returns the hexadecimal string format of the ObjectId.
  • ObjectId.getTimestamp() : It returns the timestamp portion of the object as a Date.
  • ObjectId.valueOf(): It return the hexadecimal format of a given String Literal.
  • ObjectId.toString(): This method returns ObjectId in String format in javascript representation.

1. Creating an ObjectId

We can generate a new ObjectId using the ObjectId() constructor.

newObjectId = ObjectId()

Output:

Screenshot-2026-02-10-125151

2. Getting the Timestamp from ObjectId

We can retrieve the creation timestamp of the ObjectId using the getTimestamp() method. This method returns the timestamp as a Date object.

var id =new ObjectId();     
id.getTimestamp()

Output:

Screenshot-2026-02-10-125342

3. Converting ObjectId to String

We can convert an ObjectId into its string format using the str property.

 new ObjectId().toString

Output:

Screenshot-2026-02-10-125540

4. Getting the Hexadecimal Representation

You can also use the valueOf() method to get the hexadecimal representation of the ObjectId.

new ObjectId().valueOf()

Output:

Screenshot-2026-02-10-125633

Using ObjectId for Querying

Since ObjectId is used as the default unique identifier for MongoDB documents, we can use it to efficiently query for documents by their _id.

db.student_gfg.findOne({ _id: ObjectId("5f92cbf10cf217478ba93561") })

In this query, the ObjectId is used to retrieve a document with a specific _id. Since ObjectId is indexed, these queries are very efficient.

Usage of ObjectId Vs Custom _id

  • Use ObjectId: When you need an automatically generated unique identifier with a timestamp and efficient indexing.
  • Use Custom _id: When you need to control the identifier format or when you already have a unique identifier (e.g., a UUID or an existing system-generated ID).
Comment

Explore