Difference Between ObjectId and $Oid in MongoDB
-
Features of the
ObjectID -
Difference Between
ObjectIDand$oidin MongoDB -
Insert Document in a Collection With
ObjectID -
Methods of the
ObjectID - Conclusion
MongoDB stores all the data records in individual BSON documents. An identifier is provided to uniquely identify each of the documents.
The identifier helps the user to uniquely locate the document while searching through other documents.
Queries work easily with the help of an identifier, and the results are generated accordingly.
In every collection, each document has a unique _id field that acts as its primary key. The _id default format is the ObjectID of the document.
Features of the ObjectID
An ObjectID acts as an identifier of an individual document within a particular collection. The ObjectID is a 12-byte BSON type field.
The division of the fields is as follows:
- Unix Timestamp of the individual document is represented by the first four bytes.
- Machine ID on which the MongoDB server is currently running is represented by the following three bytes.
- Process ID is represented by the next two bytes.
- To increment the
ObjectID, the last three bytes are reserved.
Format of an ObjectID
The ObjectID can be defined when a document is created. There are two ways to assign the ObjectID to a document.
- Upon creation, the user assigns the
ObjectIDto the document itself. - The document takes a default
ObjectIDassigned to it by the MongoDB server.
The ObjectID is defined in the following format:
ObjectId(<hexadecimal>)
The <hexadecimal> parameter to define an ObjectID is optional. If the user defines a value in the _id field, that value is assigned as an ObjectID to the document.
Otherwise, the MongoDB server provides an ID to the document.
Note: It must be ensured that the
ObjectIDprovided by the user should be unique for each document within the collection.
Difference Between ObjectID and $oid in MongoDB
There is no difference between an ObjectID and an $oid in the MongoDB server. The two are just different serialization formats introduced with the newer shells.
Object IDs are represented with the MongoDB server’s help of $oid. The server uses Strict MongoDB Extended JSON; therefore, the Object IDs are represented as follows:
{ "$oid": "<id>" }
As shown above, the $oid term can be used when searching for a particular document. You can use $oid while writing queries.
On the other hand, the ObjectID can be used to create documents. In addition, the methods of the ObjectID can be used to get specific values from the 12-byte long Object ID.
In short, there is not much difference between the two entities in the MongoDB server.
Insert Document in a Collection With ObjectID
As discussed in the section earlier, the ObjectID field takes an optional hexadecimal parameter. If no value for the _id field is set in the document’s creation, the MongoDB server sets a default, unique value for the document in the collection.
The syntax to insert a single document in the collection is as follows:
db.collectionName.insertOne()
The code snippet inserts one document in the collection with a default value in the _id field set by the server.
The syntax to insert multiple documents in the collection is as follows:
db.collectionName.insertMany()
The code snippet inserts several documents in the collection with a default value in the _id field set by the server.
Set the ObjectID Field
The user can set the _id field when creating a document, given that it is unique. The syntax for setting the ObjectID, or $oid in other words, is as follows:
db.collectionname.insertOne({"_id":"1789473"})
Here is an example of setting an ObjectID of a document within a collection. Assume the following entities:
- Name of the Database:
officedb - Name of the Collection:
employee_officedb
Use the following commands to create a document in the MongoDB server:
> use officedb
switched to db officedb
> db.createCollection("employee_officedb")
{ "ok" : 1 }
> db.employee_officedb.insert({ name : "asad", rank : 23})
WriteResult ({ "nInserted" : 1})
> db.employee_officedb.find().pretty()
{
"_id" : ObjectId ("8e12bn2a0ty562888ab93711"),
"name" : "asad"
"rank" : 23
}
Methods of the ObjectID
Four methods expand on the concept of ObjectID in MongoDB. Each of the methods serves a different functionality, as described below.
- The
strMethod: This is used to get the Object ID in a hexadecimal string format. - The
ObjectId.getTimestamp()method returns the object’s timestamp portion as a Date. - The
ObjectId.valueOf()Method: The hexadecimal format of a given string literal is returned by this method. - The
ObjectId.toString()Method: This method returns the object ID in a string format in JavaScript representation.
Conclusion
There is a huge room to explore the ObjectID and $oid in the MongoDB server. There is not much difference between the two, except for the representation.
Some shells make use of the ObjectID, while other shells make use of the $oid. Therefore, data manipulation and storage depend upon the version of the shell installed on your device.
For the definition of documents and their IDs, use ObjectID. On the other hand, while writing and running queries, use $oid to search through the entire collection to find the required document.
Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!
GitHub