Home > What Is MongoDB's _id Field and How to Use It

What Is MongoDB's _id Field and How to Use It

The MongoDB _id field is fundamental to every collection in MongoDB, and, by default, it has some useful properties users can take advantage of when familiar with how _id is generated. Understanding the field's default behavior and the advantages and pitfalls associated with it can be useful for managing your collections as well as understanding when to override it.   Denver SWUG

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. Each chain represents and designates a specific aspect of the document's identity.

How Does MongoDB ObjectID Work?

Architecturally, by default the _id field is an ObjectID, one of MongoDB's BSON types. The ObjectID is the primary key for the stored document and is automatically generated when creating a new document in a collection. The following values make up the full 12-byte combination of every _ID (quoted from MongoDB's documentation):
  • "a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and a 3-byte counter, starting with a random value."
These are also a few _id field principal characteristics you should know about:
  • _id is the primary key on elements in a collection; with it, records can be differentiated by default.
  • _id is automatically indexed. Lookups specifying { _id: <someval> } refer to the _id index as their guide.
  • Users can also override _id to something other than an ObjectID data type, if desired.

Is MongoDB ObjectID Unique?

According to MongoDB, ObjectID can be considered globally unique. The first nine bytes in a MongoDB _ID guarantee its uniqueness across machines and processes, in relation to a single second; the last three bytes provide uniqueness within a single second in a single process.

Are There Any Fields All Documents in MongoDB Must Have?

All documents in MongoDB must have a populated _id field. If a document hasn’t been assigned an _id value, MongoDB will automatically generate one.

Important MongoDB Commands for ObjectID

Thanks to the universal, required nature of the _id field, there are several actions available to users to simplify your organization and handling of documents.

MongoDB Get Timestamp From _id

Users who stick to the default ObjectID can access the creation of their timestamps with the getTimestamp() command, providing an easy and reliable method for seeing precisely when a document was created. Additionally, with a little client-side computation, you can leverage the built-in timestamp to find documents based on insertion time.

MongoDB Order by Timestamp

Similarly, because timestamps are included in the ID’s generation, sorting by _id (or any MongoDB ObjectID field) will also sort your documents with a rough adherence to when they were created. However, this sorting method doesn’t represent a strict or exact insertion time—other components of the ID can come into play here, causing the order to reflect other variables than just creation time.

Can You Replace _id in MongoDB?

The _id field is immutable—that is, once a document exists in your MongoDB system, it has, by definition, been assigned an _id, and you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID. Overriding the _id field for a document can be useful, but when you do so, you need  to ensure the values for each document are unique.

MongoDB ObjectID Tips

Due to factors and properties of the _id field, there are (at least!) two things to be wary of when dealing with ObjectID:
  1. Be careful when sharding with the _id index—because ObjectIDs contain timestamps, they’re generated in ascending order. Therefore, if you shard a collection by _id, make sure you use hashed sharding. Otherwise, all new inserts will go to a single shard. This is a problem MongoDB beginners frequently run into if not warned this is the default behavior. Learn more about the basics of sharding in MongoDB in this guide.
  2. Overriding the _id field means you'll need to generate a unique value for your documents. While you can use UUIDs or some other unique identifier to your application, it’s best to allow MongoDB to use its default if you aren’t certain your replacement field is unique.
Learn More About SolarWinds Hybrid Cloud Observability

Other Resources to Improve MongoDB Performance

MongoDB users are well known for valuing the database's capacity for growth. After all, the system's tagline is "for giant ideas," and for good reason. The system supports extreme scalability and has been designed to store exceptionally large numbers of documents.  Hopefully this guide on MongoDB ObjectID has helped you understand its makeup and characteristics, so you can better use the ways your database is organizing your documents. Check out other popular MongoDB guides and resources designed to help you more easily optimize and fine tune database performance:
Avatar photo
We’re Geekbuilt.® Developed by network and systems engineers who know what it takes to manage today's dynamic IT environments, SolarWinds has a deep connection to…
Read more