What is _id in mongodb?


We have been using MongoDB Object Id in all the previous chapters. In this chapter, we will understand the structure of ObjectId.

An ObjectId is a 12-byte BSON type having the following structure −

  • The first 4 bytes representing the seconds since the unix epoch
  • The next 3 bytes are the machine identifier
  • The next 2 bytes consists of process id
  • The last 3 bytes are a random counter value

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique.

Creating New ObjectId

To generate a new ObjectId use the following code −

>newObjectId = ObjectId()

The above statement returned the following uniquely generated id −

ObjectId("5349b4ddd2781d08c09890f3")

Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id −

>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")

Creating Timestamp of a Document

Since the _id ObjectId by default stores the 4-byte timestamp, in most cases you do not need to store the creation time of any document. You can fetch the creation time of a document using getTimestamp method −

>ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()

This will return the creation time of this document in ISO date format −

ISODate("2014-04-12T21:49:17Z")

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 −

>newObjectId.str

The above code will return the string format of the Guid −

5349b4ddd2781d08c09890f3

What is _id in mongodb?

Today in this article we shall see a few useful guidelines while defining the schema for MongoDB using _id Field.

Today we will cover the below aspects,

What is _id in MongoDB?

_id fields in MongoDB is an ObjectId. Itis unique, fast to generate, and ordered 12 bytes in length, consisting of timestamp + random value + counter.

ObjectId is a construct providing a globally unique identifier for your document.

How ‘_id’ is created in MongoDB

Each document stored in a MongoDB collection requires a unique _id field which is also reserved as a primary key identifying each document uniquely.

What is _id in mongodb?

For more details, please visit this article,

  • MongoDB ObjectId Data Type – Guidelines

When a new document is inserted and doesn’t specify the ‘_id’ field explicitly, then MongoDB driver automatically generates _id as ObjectId.

What is _id in mongodb?

Let’s take an example schema document that is pushed to MongoDB,

Example

{
  "Name": "Design Patterns",
  "Price": 54.93,
  "Category": "Computers",
  "Author": "Ralph Johnson"
}

MongoDB document inserted as below,

{
"_id":"5db5a4476997188b2722c820",
"Name":"Design Patterns",
"Price":54.93,
"Category":"Computers",
"Author":"Ralph Johnson"
}

With the above, you get a resolution for Database Infra level uniqueness. By no means, you will get the same document with the same “_id” in the given collection. This is solely taken care of by the MongoDB system and its default behavior.

Can I Use my own _id defined

This is very much a possibility in fact I have seen many projects using their own _id.

Example:

In the below example _id is defined by the client code explicitly and inserted to the database.

Here is an example when using custom-defined ‘_id’

What is _id in mongodb?

Before using this option there are below guidelines that could help you decide,

Guidelines and Best practices for ID field

  • By default, the _id type will be ObjectID unless specified explicitly during the creation of a document.
  • _id values need not be an ObjectID. Users can override _id to other than an ObjectID data type if desired. For example, you can define _id as String or Int as needed.
  • The _id field will always be the first field in the documents.
  • If the received document does not have the _id field as the first field, then the MongoDB server moves it to first as the default behavior.
  • _id supported types in MongoDB – _id support most of the BSON types.
  • _id remains the primary key to elements in a collection.
  • _id is automatically indexed.
What is _id in mongodb?
  • If defining your own custom _id then please be sure 100% that it will remain unique. You will get multiple errors for the duplicates, even with less than 0.009% probability as more volume records could produce more such errors.
  • Please make sure not to use your id as sensitive information due to security best practices.
  • Sensitive information like PII or PHI as your _id is not recommended but please ensure the feasibility of options before using them. It could be possible your use case might just work without any issues.

Your PII or PHI information may include name, address, SSN, Tax TIN, or other identifying number or code, telephone number, email address, Medical record numbers. etc.)

  • Using PHI or PII details as Unique ID or _Id sometimes might affect the data structure. Especially if you end up using them as encrypted due to their sensitiveness.
  • Using a custom _id means also you need to deal with at least two indexes for every Create, Read, Update or Delete operation which could result in a performance penalty if performing write-heavy operations.

That’s all! Hope you find these guidelines helpful.

References:

  • MongoDB Naming Conventions and Standards

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribeto the blog to get a notification on freshly published best practices and guidelines for software design and development.


To serve the best user experience on website, we use cookies . By clicking “Accept”, you give consent to our privacy policy.

Why do we use _id in MongoDB?

Every document in the collection has an “_id” field that is used to uniquely identify the document in a particular collection it acts as the primary key for the documents in the collection. “_id” field can be used in any format and the default format is ObjectId of the document.

What is meaning of _id in MongoDB?

What Is MongoDB ObjectID? As MongoDB documentation explains, "ObjectIds are small, likely unique, fast to generate, and ordered." The _id field is a 12-byte Field of BSON type made up of several 2-4 byte chains and is the unique identifier/naming convention MongoDB uses across all its content.

How is _id generated in MongoDB?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record.

What is difference between id and _id in MongoDB?

The _id field in MongoDB document database is considered to be the default field for BSON ObjectId's and it is, by default, indexed. _id and id are not the same. You may also prefer to add a field called id if you want, but it will not be index unless you add an index.