If You are interested to learn about the advanced Indexing in MongoDB
A 12-byte BSON type called an ObjectId has the following structure:
- seconds since the Unix epoch are represented by the first 4 bytes.
- The machine identifier is the following 3 bytes.
- The process id is contained in the next 2 bytes.
- A random counter value is present in the final 3 bytes.
Each document’s _id field, which is produced during document creation, uses ObjectIds by default in MongoDB. All of the _id fields are distinct due to the intricate arrangement of ObjectId.
Creating New ObjectId
To generate a new ObjectId use the following code −
>newObjectId = ObjectId()
The above statement returned the following uniquely generated id −
Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id −
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
How is ObjectId generated?
The database drivers will automatically construct an ObjectID and assign it to the _id column of each document. For all intents and purposes, ObjectID can be regarded as globally unique. For searches or to sort by creation time, ObjectID encodes the timestamp of its creation time.
How does MongoDB obtain the ID value?
Finding the document that matches the user-specified “id” is made possible by the MongoDB function findById(). MongoDB uses the find() function to implement findById. It returns null if the supplied “id” cannot be found in any documents.
Creating Timestamp of a Document
In most circumstances, you do not need to store the creation time of any document because the _id ObjectId by default stores the 4-byte timestamp. With the help of the getTimestamp method, you may discover when a document was created.
This will return the creation time of this document in ISO date format −
Converting ObjectId to String
In some cases, you may need the value of ObjectId in a string format. To convert the ObjectId in string, use the following code −
The above code will return the string format of the Guid −
Creating a new ObjectId:
You can declare ObjectId as a method in MongoDB to manually construct a new Object ID. You can see that we are declaring a variable with the ObjectId method as its value in the image below. A distinct hexadecimal will be returned, which we may put in a variable called “myObjectId.”
- Command: myObjectId = ObjectId()
In this example, the value of myObjectId would be: ObjectId(“6009cb85e65f6dce28fb3e51”).
In the above image, you can observe that each time it is returning a unique hexadecimal value.
Specify a Hexadecimal String:
MongoDB gives you the option to define your own particular hexadecimal value on occasion. The produced hexadecimal value is shown in the sample above.
In this case, the ObjectId method’s parameter will be an object ID with a hexadecimal value.
- Command: newObjectId = ObjectId(“507f1f77bcf86cd799439011”)
In this example, the value of newObjectId would be: ObjectId(“507f1f77bcf86cd799439011”).
Access the Hexadecimal String:
In the above examples we are unable to get the hexadecimal string, as it will return you the whole method having the unique hexadecimal value.
To extract the unique hexadecimal as a string from the Object ID, we need to use ‘str’ attribute which is available in ObjectId method.
- Command: ObjectId(“507f191e810c19729de860ea”).str
In this example, the value of the ObjectId(“507f191e810c19729de860ea”).str method returned the hexadecimal string which is inside ObjectId method.
Methods available upon ObjectId:
- Note: We already have newObjectId with us so we can test these methods using the same variable.
- Method: ObjectId.getTimestamp().
- Explanation: Returns the timestamp portion of the object as a Date.
- Command: newObjectId.getTimestamp()
This will return the creation time of this document in ISO date format.
- Method: ObjectId.toString()
- Explanation: Returns the string representation of the ObjectId(). This string value has the format of ObjectId(<hexadecimal>).
- Command: newObjectId.toString()
This will return the ObjectId(<hexadecimal>) of this document in string format.
- Method: ObjectId.valueOf()
- Explanation: Returns the value of the ObjectId() as a lowercase hexadecimal string. This value is the str attribute of the ObjectId() object.
- Command: newObjectId.valueOf()