0% completed
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.
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.
An ObjectId is a 12-byte (96-bit) hexadecimal value divided into four parts:
4 bytes represent the seconds since the Unix epoch (January 1, 1970). This allows for easy extraction of the document's creation time.3 bytes represent a unique identifier of the machine where the ObjectId was generated. This helps in differentiating ObjectIds generated on different machines.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.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.Here is an example of an ObjectId and a breakdown of its components:
ObjectId("507f1f77bcf86cd799439011")
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 })
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.
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);
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.
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 })
.....
.....
.....