Difference between ObjectId and $Oid in MongoDB
MongoDB stores all data records in separate BSON documents. An identifier is provided to uniquely identify each document.
The identifier helps users uniquely locate the document when searching for other documents.
With the help of identifiers, queries can be easily performed and results can be generated accordingly.
In each collection, each document has a unique _id
field as its primary key. _id
The default format is the document's ObjectID.
ObjectID Features
An ObjectID acts as an identifier for a single document within a particular collection. An ObjectID is a 12-byte BSON type field.
The fields are divided as follows:
- The Unix timestamp of a single document is represented by the first four bytes.
- The machine ID of the current MongoDB server is represented by the following three bytes.
- The process ID is represented by the next two bytes.
- For incrementing the ObjectID, the last three bytes are reserved.
ObjectID format
You can define an ObjectID when you create a document. There are two ways to assign an ObjectID to a document.
- Upon creation, the user assigns an ObjectID to the document itself.
- The document uses the default ObjectID assigned to it by the MongoDB server.
ObjectIDs are defined in the following format:
ObjectId(<hexadecimal>)
The parameter used to define the ObjectID <hexadecimal>
is optional. If the user _id
defines a value in the field, that value will be assigned as the ObjectID to the document.
Otherwise, the MongoDB server assigns an ID to the document.
Difference between ObjectID and $oid in MongoDB
There is no difference between ObjectID and $oid in MongoDB server. They are just different serialization formats introduced by the new shell.
Object IDs are represented with the help of $oid in the MongoDB server. The server uses Strict MongoDB Extended JSON; therefore, object IDs are represented as follows:
{ "$oid": "<id>" }
As shown above, you can use the $oid term when searching for a specific document. You can use $oid when writing queries.
On the other hand, ObjectID can be used to create documents. In addition, ObjectID methods can be used to get specific values from the 12-byte Object ID.
In conclusion, there is not much difference between these two entities in MongoDB server.
Insert a document into a collection using ObjectID
As mentioned in the previous section, the ObjectID field takes an optional hexadecimal parameter. If you do not _id
set a value for the field when you create a document, the MongoDB server assigns a default unique value to the documents in the collection.
The syntax for inserting a single document into a collection is as follows:
db.collectionName.insertOne()
This code snippet inserts a document into the collection with _id
the default value in the field set by the server.
The syntax for inserting multiple documents into a collection is as follows:
db.collectionName.insertMany()
This code snippet inserts multiple documents into a collection and _id
uses default values in the fields of the server settings.
Set the ObjectID field
The user can set the field when creating a document _id
because it is unique. The syntax for setting ObjectID or $oid is as follows:
db.collectionname.insertOne({"_id":"1789473"})
Here is an example of setting the ObjectID of a document in a collection. Assume the following entity:
- Database name: officedb
- Set name: employee_officedb
Use the following command 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
}
ObjectID methods
Four methods extend the concept of ObjectID in MongoDB. Each method has different functionality, as described below.
- str Method: This is used to get the object id in hexadecimal string format.
- The ObjectId.getTimestamp() method returns the timestamp portion of an object as a date.
- ObjectId.valueOf() method: This method returns the hexadecimal format of the given string literal.
- ObjectId.toString() method: This method returns the object ID in a string format represented by JavaScript.
Summarize
There is a lot of room to explore ObjectID and $oid in MongoDB server. There is not much difference between the two except the representation.
Some shells use ObjectID, while others use $oid. Therefore, data manipulation and storage depends on the version of the shell installed on your device.
For the definition of documents and their IDs, use ObjectID. On the other hand, when writing and running queries, use $oid to search the entire collection to find the required document.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
List all collections in MongoDB Shell
Publish Date:2025/04/29 Views:156 Category:MongoDB
-
When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c
Querying for non-null values in MongoDB
Publish Date:2025/04/29 Views:139 Category:MongoDB
-
This MongoDB article will explain how to query for non-null values in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers
Shutting down with code:100 error in MongoDB
Publish Date:2025/04/29 Views:53 Category:MongoDB
-
This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know
SELECT COUNT GROUP BY in MongoDB
Publish Date:2025/04/29 Views:74 Category:MongoDB
-
In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation
Differences between MongoDB and Mongoose
Publish Date:2025/04/29 Views:80 Category:MongoDB
-
This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s
Install MongoDB using Homebrew
Publish Date:2025/04/29 Views:161 Category:MongoDB
-
MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a
Create a MongoDB dump of the database
Publish Date:2025/04/29 Views:62 Category:MongoDB
-
In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t
Get the size of the database in MongoDB
Publish Date:2025/04/29 Views:88 Category:MongoDB
-
When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbs the command and the method. db.stats() Get the size of the database in MongoDB We can show dbs;
Grouping values by multiple fields with MongoDB
Publish Date:2025/04/29 Views:99 Category:MongoDB
-
MongoDB Group by Multiple Fields is used to group values by multiple fields using various methods. One of the most efficient ways to group various fields present in MongoDB documents is by using $group the operator, which helps in per