Role of Object ID() and its Methods in MongoDB:
In this tutorial, we will learn about ObjectId and its methods within MongoDB.
Basically, ObjectId is treated as the primary key within any MongoDB collection.
It is generated automatically whenever we create a new document within a new collection. It is based on a 12-byte hexadecimal value as you can observe in the following syntax.
This hexadecimal value is divided into three segments which are as follows.
- First segment of 4-byte value represents the seconds since the UNIX epoch.
- Second segment of 5-byte random value.
- Third segment of 3-byte counter starts with a random value.
MongoDB provides three methods for ObjectId as shown below:
What You Will Learn:
To create a new objectID manually within the MongoDB you can declare objectId as a method. In simple words, we can say that object ID is a unique identifier for each record. In the below image you can observe that we are declaring a variable having object ID method as a value and it will return unique hexadecimal.
X = ObjectId()
Figure 1: In Mongo Shell
Figure 2: In Robo 3T
In the above image, you can observe that we are declaring an objectID method as a value for “x” variable multiple times. Each time it will return a unique hexadecimal value and when we call only the “x”, it will return the last stored hexadecimal value.
From this experiment, we get to know that, each time when we call ObjectID, it will reserve a specific location within the virtual memory for a record.
Define Specific ObjectId Hexadecimal
If you want to define your own unique hexadecimal value then MongoDB will enable you to perform this action. In the above example, it's just declaring the object ID without any parameter as a method.
In this scenario, we will define an object ID with a hexadecimal value as a parameter of the method.
As you can observe, in the following image we are defining the value of “y” variable as a predefined object ID. Thus it will return the same object ID by which we get to know that we can specify a particular hexadecimal value for an individual object ID.
y = ObjectId(“5bf142459b72e12b2b1b2cd”)
Figure 3: In Mongo Shell
Figure 4: In Robo 3T
Get ObjectId Hexadecimal String
When you call the Object ID, it will not return you the string, rather it will return you the whole method having the unique hexadecimal value.
To extract the unique hexadecimal as a string from the objectID, you have to use “.str” as postfix. In the following image, you can observe how it works.
y = ObjectId(“5bf142459b72e12b2b1b2cd”).str
Figure 5: In Mongo Shell
Figure 6: In Robo 3T
Object ID is treated as a primary key within the MongoDB collection and is generated automatically. We can generate an object ID manually and we can also define a hexadecimal value according to our requirement.
We can also extract the hexadecimal value as a string from the object ID. The object ID is a combination of time, random value and counter value. Each time when we call the Object ID, it creates a unique hexadecimal value.
Get ready to explore the Usage of find() in MongoDB Query Document in our upcoming tutorial.