Difference Between ObjectId and $Oid in MongoDB

Bilal Shahid Dec 16, 2022
  1. Features of the ObjectID
  2. Difference Between ObjectID and $oid in MongoDB
  3. Insert Document in a Collection With ObjectID
  4. Methods of the ObjectID
  5. Conclusion
Difference Between ObjectId and $Oid in MongoDB

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:

  1. Unix Timestamp of the individual document is represented by the first four bytes.
  2. Machine ID on which the MongoDB server is currently running is represented by the following three bytes.
  3. Process ID is represented by the next two bytes.
  4. 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.

  1. Upon creation, the user assigns the ObjectID to the document itself.
  2. The document takes a default ObjectID assigned to it by the MongoDB server.

The ObjectID is defined in the following format:


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 ObjectID provided 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:


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:


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:


Here is an example of setting an ObjectID of a document within a collection. Assume the following entities:

  1. Name of the Database: officedb
  2. 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.

  1. The str Method: This is used to get the Object ID in a hexadecimal string format.
  2. The ObjectId.getTimestamp() method returns the object’s timestamp portion as a Date.
  3. The ObjectId.valueOf() Method: The hexadecimal format of a given string literal is returned by this method.
  4. The ObjectId.toString() Method: This method returns the object ID in a string format in JavaScript representation.


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.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

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!


Related Article - MongoDB ObjectId