Auto-Increment Sequence in MongoDB
MongoDB does not have the auto-increment out-of-the-box feature like SQL Database. By default, it uses a 12-byte ObjectId for the _id field as the Primary key to uniquely identify the Documents. However, there are situations when we want the _id field to have some values that automatically increase beyond ObjectId.
MongoDB does not have the auto-increment out-of-the-box feature like SQL Database. By default, it uses a 12-byte ObjectId for the _id field as the Primary key to uniquely identify the Documents. However, there are situations when we want the _id field to have some values that automatically increase beyond ObjectId.
Since this is not the default feature in MongoDB, we will program this feature using a counters collection as suggested by MongoDB Documentation.
Use counter Collection
You follow the following product document. We want the _id field to be an ascending sequence of integers starting from 1, 2, 3, 4 to n.
{ "_id" : 1 , "product_name" : "Apple iPhone" , "category" : "mobiles" }
To do this, create a counters collection to track the final range value for all sequence fields.
> db . createCollection ( "counters" )
We will now insert the following Document in counters Collection with its productid key.
{ "_id" : "productid" , "sequence_value" : 0 }
Field sequence_value tracks the last value of that sequence.
Use the following code to insert this sequence document into counters collection:
> db . counters . insert ({ _id : "productid" , sequence_value : 0 })
Create JavaScript function
Now, we will create a getNextSequenceValue function, which accepts the array name as its input, increments the array value by 1 and returns the updated number. In this example, the array name is the productid .
> function getNextSequenceValue ( sequenceName ){ var sequenceDocument = db . counters . findAndModify ( { query :{ _id : sequenceName }, update : { $inc :{ sequence_value : 1 }}, new : true }); return sequenceDocument . sequence_value ; }
Use JavaScript function
Now we use the getNextSequenceValue function while creating a new Document and assigning the returned range value as the Document's _id field.
Insert two Document templates by using the following code:
> 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 used the getNextSequenceValue function to set the value for the _id field.
To test this feature, we retrieve the Documents using the find command:
> db . prodcuts . find ()
The query returns documents with _id fields that are automatically incremented.
{ "_id" : 1 , "product_name" : "Apple iPhone" , "category" : "mobiles" } { "_id" : 2 , "product_name" : "Samsung S3" , "category" : "mobiles" }
According to Tutorialspoint
Last lesson: Working with Rockmongo
Next article: GridFS in MongoDB
You've just finished reading the article "Auto-Increment Sequence in MongoDB" edited by the TipsMake team. You can save autoincrement-sequence-in-mongodb.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.