MongoDB using _id Field – Guidelines

MongoDB id Field

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. It is 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.

For more details, please visit this article,

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

mongodb id field best practices

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’

mongo id best practices

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

Guidelines and Best practices for MongoDB 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.

  • 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 possible but please ensure the feasibility of such options before using them. It could be possible your use case might work fine or will not be addressed correctly.

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:

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 Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *