Updated February 27, 2023
Introduction to MongoDB ObjectId()
MongoDB objectid() returns a new objectid value, objectid in MongoDB consisting of 4-byte timestamp value which represented an objectid creation and is measured in seconds. Objectid is very important and useful to return a new objectid value, objectid in MongoDB consisting of 12-byte random value. A 3 byte incrementing counter is used to initialize random value, objectid in MongoDB will accept the hexadecimal string value for the new objectid. The hexadecimal parameter is an optional parameter that was used with objectid, type of hexadecimal parameter is a string.
Syntax and Parameter
In the below syntax hexadecimal value is divided into three segments in MongoDB.
- The first segment contains the 4-byte value, which represented the second since the UNIX epoch in MongoDB.
- The second segment will contain 5-byte random value.
- The third segment will contain 3 byte counter starts with a random value.
Objectid (<hexadecimal>)
Below is the parameter description of MongoDB objectid syntax.
- Objectid: Objectid is very important and useful in MongoDB to return a new objectid value. Objectid in MongoDB was contained three methods is get a timestamp, to String and value of. To create a new objectid in MongoDB, we need to declare objectid as the method. This we can define objectid as a unique identifier for each record.
- Hexadecimal: This parameter is essential and useful in MongoDB to define hexadecimal value. Hexadecimal in MongoDB objectid define as value of the variable, we can define a variable in the place of a hexadecimal value. Each time declared variable using objectid in MongoDB would return a unique hexadecimal value.
How ObjectId() works in MongoDB?
Below is the working of objectid is as follows. This was basically provided three methods of objectid.
- gettimestamp()
- toString()
- Valueof()
1. The first method is gettimestamp it will contain a timestamp. It is an essential and useful method of objectid. It will return the timestamp portion of the objectid.
2. The second method will contain a toString; it will convert the string. MongoDB toString will return the string representation of objectid.
- The value of the method in objectid will returns a lowercase hexadecimal string in MongoDB. This value will contain str attribute of objectid.
- We can declare a variable with objectid. The below example shows declare objectid.
A = objectid ()
- The objectid is nothing but default primary key of the document, which was usually found in the id document field at the inserted document.
- This objectid will contains 12-byte binary BSON type which contained 12 bytes. The driver and server will generate objectid using a default algorithm.
- Objectid is very important and useful in MongoDB to return a new objectid value, objectid in MongoDB consist of 12-byte random value.
- The hexadecimal parameter is an optional parameter that was used with objectid, type of hexadecimal parameter is a string.
- The objectid() is return a new objectid value, objectid in MongoDB consists of 4-byte timestamp value which represented an objectid creation and measured in seconds.
- A 3 byte incrementing counter is used to initialize random value, objectid will accept the hexadecimal string value for the new objectid.
- If we want to define our own hexadecimal value in MongoDB, it will enable definer to define hexadecimal value.
- This we can define objectid with hexadecimal value as a parameter or a method. We can also define objectid as a method in MongoDB. Objectid is also known as a unique identifier.
- MongoDB objectid will create automatically when we have inserted a new document within the collection.
Examples to Implement MongoDB ObjectId()
Below are the examples mentioned:
Example #1 – Create objectid at the time of document insertion
Below example states that create objectid at the time of document insertion. At the time of document insertion, objectid will automatically be generated.
db.mongo_objectid.insert({publisher_name: "XYZ", publication_year:2020, location: "Mumbai", books_id: [100, 200]})
db.mongo_objectid.insert({publisher_name: "ABC", publication_year:2020, location: "Mumbai", books_id: [100, 200]})
db.mongo_objectid.insert({publisher_name: "PQR", publication_year:2020, location: "Mumbai", books_id: [300, 500]})
db.mongo_objectid.find()
Output:
Explanation: In the above example, we have inserted three documents. But we have not inserted an objectid field. Objectid field will automatically be created at the time of document insertion.
Example #2 – Generate new objectid
The below example shows create new objectid. At the time of creating a new objectid, we have to define A as variable.
Code:
A = ObjectId()
Output:
Example #3 – Specify a hexadecimal string
In the below example, we have to define the hexadecimal string. The hexadecimal string will create the object. The hexadecimal string will return the same hexadecimal string which we have to define in the example.
Code:
A = ObjectId("757f191a810c19729de860ae")
Output:
Example #4 – Access hexadecimal string
The below example is an objectid that access hexadecimal string using an str attribute. It will return hexadecimal value using an str attribute.
Code:
ObjectId ("807f191a810c19729de860ae").str
Output:
Example #5 – Objectid using gettimestamp
In the below example, we have called the gettimestamp method to generate objectid. Gettimestamp is a handy and important method to generate objectid.
Code:
ObjectId("617a7f79bcf86ef7994f6c0a").getTimestamp()
Output:
Example #6 – Objectid using toString
The below example shows objectid using the toString method. In the below example, we have called the toString method to generate objectid, toString is a handy and important method to generate objectid.
Code:
ObjectId("617a7f79bcf86ef7994f6c0a").toString()
Output:
Example #7 – Objectid using valueOf
The below example shows objectid using the valueOf method. In the below example, we have called the valueOf method to generate objectid in MongoDB, valueOf is a handy and important method to generate objectid.
Code:
ObjectId("617a7f79bcf86ef7994f6c0a").valueOf()
Output:
Conclusion
Objectid is very important to return new objectid value, objectid consist of 12-byte random value. Thus objectid() is return a new objectid value, objectid in consisting of 4-byte timestamp value which represented an objectid creation and measured in seconds.
Recommended Articles
This is a guide to MongoDB ObjectId(). Here we discuss an introduction to MongoDB ObjectID() with syntax, parameters, and examples to better understand. You can also go through our other related articles to learn more –