Navicat Blog

Mar 26, 2019 by Robert Gravelle

Open up any document in a MongoDB database and you'll notice an _id field:


In fact, the ObjectId/_id is the only field that exists across every MongoDB document. In today's blog, we'll explore what it is and why it's important to your MongoDB database.

The Structure of ObjectId

As a quick, opening summary, these are a few of _id's principal characteristics:

  • _id is the primary key on documents in a collection; with it, documents (records) can be differentiated from each one another.
  • _id is automatically indexed. Lookups specifying { _id: <someval> } refer to the _id index as their guide.
  • By default the _id field is of type ObjectID, one of MongoDB's BSON types. Users can also override _id to something other than an ObjectID, if desired.

ObjectIDs are 12 bytes long, composed of several 2-4 byte chains. Each chain represents and designates a specific aspect of the document's identity. The following values make up the full 12 byte combination:

  • a 4-byte value representing the seconds since the Unix epoch
  • a 3-byte machine identifier
  • a 2-byte process id
  • a 3-byte counter, starting with a random value

Typically, you don't have to concern yourself with generating the ObjectID. If a document has not been assigned an _id value, MongoDB will automatically generate one.

Creating New ObjectId

If you want to generate a new ObjectId yourself, you can use the following code:

newObjectId = ObjectId()

You can also type it directly into the Navicat editor.

That will generate a unique _id such as:

ObjectId("5349b4ddd2781d08c09890f3")

Alternatively, you can 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("2019-09-12T30:39: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

Document Sorting

Since each ObjectId contains a timestamp, you can sort your documents by _id to by create time. Be sure to note, however, that this sorting method does not represent a strict or exact ordering, because other components of the ID can come into play, causing the order to reflect other variables than just creation time.

Changing the ObjectId

The _id field is basically immutable so that, after a document is created, it has, by definition, been assigned an _id, which cannot be changed. Having said that, the _id can be overridden when you insert new documents. Overriding the _id field for a document can be useful, but when you do so, you are responsible for ensuring that the values for each document are unique.

Conclusion

MongoDB's _id field plays a vital role in every MongoDB collection. Therefore, understanding how it's created as well as when to override it can be useful for managing your collections.

If you'd like to learn more about Navicat for MongoDB, please visit the product page. Do you work with many database types? Navicat Premium 12.1 also supports MongoDB!

Navicat Blogs
Feed Entries
Blog Archives
Share