The document stored in MongoDB must have a "_id" key. The value of this key can be any type, and the default is a Objectid object.
ObjectId is a 12-byte BSON type data that has the following format:
- The first 4 bytes represent a timestamp
- The next 3 bytes are the machine identification code
- The immediate two bytes consist of a process ID (PID)
- The last three bytes are a random number.
Within a collection, each collection has a unique "_id" value to ensure that each document within the collection is uniquely identified.
MongoDB uses Objectid, not the main reason for other, more conventional practices, such as auto-incremented primary keys, because it is laborious and time consuming to automatically increase primary key values on multiple servers.
While MongoDB does not have auto-growing capabilities like SQL, in some cases we may need to implement OBJECTID auto-grow.
Since MongoDB does not implement this function, we can do it programmatically, the following we will implement the _id field automatically grow in the Counters collection.
Using the Counters Collection
Consider the following products documentation. We want the _id field to implement the auto-grow function from 1,2,3,4 to N. To do this, create a counters collection where the sequence field values can be automatically long
>db.createcollection ("counters")
The Sequence_value field is a value after the sequence is automatically grown.
Use the following command to insert the counters collection in the sequence document:
>db.counters. Insert ({_id: "ProductID", Sequence_value:0})
Creating Javascript Functions
Now, we create the function Getnextsequencevalue as input to the sequence name, and the specified sequence automatically grows by 1 and returns the latest sequence value. In the example in this article, the sequence is named ProductID.
> function Getnextsequencevalue (sequencename) { var= db.counters.findAndModify ( { Query:{_id:sequencename}, Update: {$inc: {sequence_value:1}}, new:true }); return Sequencedocument.sequence_value;}
Using Javascript functions
Next we will use the Getnextsequencevalue function to create a new document and set the document _ID automatically for the returned sequence value:
>db.products. Insert ({ "_id": Getnextsequencevalue ("ProductID"), "Product_Name": "Apple iPhone", "category": "Mobiles" })>db.products. Insert ({ "_id": Getnextsequencevalue ("ProductID"), "Product_Name": "Samsung S3", "category": "Mobiles" })
As you can see, we use the Getnextsequencevalue function to set the _id field.
To verify that the function is valid, we can read the document using the following command:
>db.prodcuts.find ()
The above command will return the following results, and we find that the _id field is self-growing:
12, "product_name": "Samsung S3", "category": "Mobiles"}
ObjectId is a 12-byte BSON type data that has the following format:
- The first 4 bytes represent a timestamp
- The next 3 bytes are the machine identification code
- The immediate two bytes consist of a process ID (PID)
- The last three bytes are a random number.
The document stored in MongoDB must have a "_id" key. The value of this key can be any type, and the default is a Objectid object.
Within a collection, each collection has a unique "_id" value to ensure that each document within the collection is uniquely identified.
MongoDB uses Objectid, not the main reason for other, more conventional practices, such as auto-incremented primary keys, because it is laborious and time consuming to automatically increase primary key values on multiple servers.
MongoDB field Growth