We have been using MongoDB Object Id in all the previous chapters. In this chapter, we will understand the structure of ObjectId. Show
An ObjectId is a 12-byte BSON type having the following structure −
MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique. Creating New ObjectIdTo generate a new ObjectId use the following code − >newObjectId = ObjectId() The above statement returned the following uniquely generated id − ObjectId("5349b4ddd2781d08c09890f3") Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id − >myObjectId = ObjectId("5349b4ddd2781d08c09890f4") Creating Timestamp of a DocumentSince 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("2014-04-12T21:49:17Z") Converting ObjectId to StringIn 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 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, _id fields in MongoDB is an ObjectId. Itis 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 MongoDBEach 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,
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 definedThis 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’ Before using this option there are below guidelines that could help you decide, Guidelines and Best practices for ID field
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 !!
To serve the best user experience on website, we use cookies . By clicking “Accept”, you give consent to our privacy policy. Why do we use _id in MongoDB?Every document in the collection has an “_id” field that is used to uniquely identify the document in a particular collection it acts as the primary key for the documents in the collection. “_id” field can be used in any format and the default format is ObjectId of the document.
What is meaning of _id in MongoDB?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.
How is _id generated in MongoDB?MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record.
What is difference between id and _id in MongoDB?The _id field in MongoDB document database is considered to be the default field for BSON ObjectId's and it is, by default, indexed. _id and id are not the same. You may also prefer to add a field called id if you want, but it will not be index unless you add an index.
|