MongoDB is a database that has gained a lot of popularity in recent times, its database growth capability and object-friendly modeling is attracting more and more adherents. After all, the system's slogan is "for giant ideas" and for good reason. The system supports extreme scalability and is designed to store an exceptionally large number of documents.

If you are starting or already using a MongoDB database, you may have noticed the obligatory existence of a curious field: _id . Do you know what it's really for and what's hidden among so many letters and numbers?

ObjectId Features

The _id field is a data of type ObjectId, and it has some primary standard functions:

  • _id is the primary key of all elements of a collection, this allows records to be differentiated by default.
  • _id is an automatically indexed field. Searches that specify {_id: } refer to the _id index as a guide.
  • In terms of architecture, by default the _id field is an ObjectID, one of the types BSON from MongoDB. Users can also replace _id with something other than an ObjectID, although it's not really recommended.

One of the reasons ObjectIds are generated in the way mentioned above is that they contain useful behavior due to the way sorting works. As it contains a 4-byte timestamp (seconds resolution) and an increment counter, as well as some more unique identifiers such as the machine identifier, thanks to this we can use the _id field to sort documents in order of creation, just order the _id field. This can be useful in databases that need to save space and do not need an additional creation timestamp.

ObjectId Structures

An ObjectId is a 12-byte binary BSON type represented in 24 hexadecimal characters:

{
    "_id": ObjectId("54759eb3c090d83494e2d804")
}

These bytes are automatically generated and separated into groups with specific functionality:

SizeDescription
4 bytes4 bytes representing seconds since Unix times
3 bytes3-byte with machine identifier
2 bytes2-byte with the unique identifier of the process
3 bytes3-byte counter starting with a random number per collection

References:
https://mongodb.github.io/node-mongodb-native/2.0/tutorials/objectid/
https://mongodb.github.io/mongo-csharp-driver/2.6/apidocs/html/T_MongoDB_Bson_ObjectId.htm
https://www.vividcortex.com/blog/what-is-mongodbs-id-field-and-how-to-use-it

4 1 voto
Nota do Artigo
Subscribe
Notify of
guest

0 Comentários
newest
oldest most voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x