Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Auto-increment Sequence in Mongodb

Understand Auto-increment Sequence in Mongodb with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts...

Table of Contents

Auto-increment Sequence in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

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 () 

FAQ

What should you know about 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.

What should you know about 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.

What should you know about 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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.