Learning MongoDB

0% completed

Previous
Next
What is ObjectId in MongoDB?

In MongoDB, every document stored in a collection requires a unique identifier. This identifier is known as the _id field. If you do not explicitly specify an _id field when inserting a document, MongoDB will automatically generate one for you. The default type for this automatically generated _id is ObjectId.

Understanding ObjectId is crucial as it plays a significant role in ensuring the uniqueness and efficiency of document retrieval in MongoDB.

What is ObjectId?

ObjectId is a 12-byte identifier typically used for the _id field in MongoDB documents. It ensures the uniqueness of each document within a collection and across different collections and databases. ObjectId is designed to be globally unique and includes a timestamp component, which allows for the creation time of the document to be easily determined.

Structure of ObjectId

An ObjectId is a 12-byte (96-bit) hexadecimal value divided into four parts:

  1. Timestamp: The first 4 bytes represent the seconds since the Unix epoch (January 1, 1970). This allows for easy extraction of the document's creation time.
  2. Machine Identifier: The next 3 bytes represent a unique identifier of the machine where the ObjectId was generated. This helps in differentiating ObjectIds generated on different machines.
  3. Process Identifier: The next 2 bytes represent the process ID (PID) of the process that generated the ObjectId. This ensures uniqueness across different processes running on the same machine.
  4. Counter: The last 3 bytes are a counter, starting with a random value. This ensures uniqueness for ObjectIds generated within the same second by the same machine and process.

Example of ObjectId

Here is an example of an ObjectId and a breakdown of its components:

ObjectId("507f1f77bcf86cd799439011")
  • 507f1f77: Timestamp component.
  • bcf86c: Machine identifier.
  • d799: Process identifier.
  • 439011: Counter.

Creating ObjectId in MongoDB

MongoDB automatically generates an ObjectId for the _id field if none is provided when inserting a document:

db.myCollection.insertOne({ name: "John Doe", age: 30 })

If you want to explicitly create an ObjectId, you can use the ObjectId() function:

var myId = ObjectId(); db.myCollection.insertOne({ _id: myId, name: "John Doe", age: 30 })

Retrieving Timestamp from ObjectId

You can extract the timestamp from an ObjectId to determine when the document was created. This can be useful for auditing and debugging purposes.

Example:

let myId = ObjectId("507f1f77bcf86cd799439011"); let timestamp = myId.getTimestamp(); print(timestamp);

This will output the creation date and time of the ObjectId.

Converting ObjectId to String

Sometimes, you may need to convert an ObjectId to a string, especially when dealing with systems that do not support BSON. This can be done using the toString() method:

let myId = ObjectId(); let idString = myId.toString(); print(idString);

Querying by ObjectId

When querying documents by their _id field, you can use the ObjectId directly in your queries. Here, find() method is used to find the document by its id. We will explore the find() method in the next chapter.

db.myCollection.find({ _id: ObjectId("507f1f77bcf86cd799439011") })

This query will return the document with the specified ObjectId.

Converting String to ObjectId

If you have an ObjectId in string format and need to convert it back to ObjectId type for querying, you can do so using the ObjectId constructor:

let idString = "507f1f77bcf86cd799439011"; let myId = ObjectId(idString); db.myCollection.find({ _id: myId })

Benefits of Using ObjectId

  1. Uniqueness: Guarantees unique identifiers across different machines and processes without requiring a central authority.
  2. Efficiency: The 12-byte size is relatively small, making it efficient in terms of storage and indexing.
  3. Sortable: The timestamp component makes ObjectIds naturally sortable by creation time.
  4. Scalability: ObjectIds can be generated independently on different machines, which is crucial for distributed systems.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next