MongoDB _id field of meaning and importance of MongoDB database

Navicat Premium is a database development tool that allows you to connect simultaneously from a single application to MySQL, MariaDB, MongoDB, SQL Server, Oracle, PostgreSQL and SQLite databases. , Amazon Aurora, Amazon Redshift, Microsoft Azure, Oracle Cloud, Google Cloud and cloud databases such as MongoDB Atlas compatible with Amazon RDS.

Open any document in MongoDB database, you will notice _id field:

Navicat Tutorial: _id field of about MongoDB

In fact, ObjectId / _id every MongoDB document unique field. In today's article, we will explore what it means and its importance MongoDB database.

ObjectId structure

As a quick summary, development, here are some of the main features _id:

  • _id is the primary key documents in the collection; with it, the document (recording) can be distinguished from each other.

  • _id automatically indexed. Specify {_id:

  • By default, the type _id field is ObjectID, is one of BSON type of MongoDB. If necessary, users can also _id coverage to anything other than the ObjectID.

ObjectID length is 12 bytes, the bytes 2-4 of the chains. Each chain representatives and specify a particular aspect of the document identifier. The following combinations of values ​​of 12 bytes to form a complete:

  • A 4-byte value, the number of seconds since the Unix era

  • A 3-byte machine identifier

  • A 2-byte process ID

  • Random beginning byte counter value 3

Navicat Tutorial: _id field of about MongoDB

Normally, you do not have to worry about generating ObjectID. If the document is not assigned the value of _id, MongoDB will automatically generate a.

Create a new ObjectId

If you want to own a new generation of ObjectId, you can use the following code:

newObjectId = ObjectId()

You can also type it directly in Navicat editor.

This generates a unique _id, for example:

ObjectId(“ 5349b4ddd2781d08c09890f3”)

Alternatively, you can provide a 12-byte ID:

myObjectId = ObjectId(“ 5349b4ddd2781d08c09890f4”)

Creation timestamp documents

Since _id ObjectId default stamp is stored 4 bytes in the case, so in most cases you do not need to store any document created time. You can use getTimestamp method to obtain the creation time of the document:

ObjectId(“ 5349b4ddd2781d08c09890f4”)。getTimestamp()

This will create the ISO date format returns the time this document

ISODate(“ 2019-09-12T30:39:17Z”)

The ObjectId converted to a String

In some cases, you may need to format string ObjectId value. To convert ObjectId to a string, use the following code:

newObjectId.str

The above code will return Guid string format:

5349b4ddd2781d08c09890f3

Document classification

Since each ObjectId contains a timestamp, so you can sort the documents by _id, you can also sort by file creation time. However, it is important to note that this method does not represent the sort of strict or precise sort of ID because other components may work, causes the command to reflect not only the creation time, also reflects other variables.

Change ObjectId

_id field is essentially immutable, therefore, after you create a document, by definition, it has allocated _id, the ID can not be changed. Having said that, _id may be overwritten when you insert a new document. _Id field covered by the document may be useful, but when doing so, you have a responsibility to ensure that the value of each document is unique.

in conclusion

MongoDB's _id field plays a vital role in every MongoDB collection. Therefore, to understand how to create and when it is useful for managing a collection of cover.


Guess you like

Origin blog.51cto.com/14467432/2456059