In a relational database, setting a field to auto-increment a number so that its value increases automatically every time you insert a new record is a readily available feature. On the other hand, NoSQL databases such as MongoDB let applications manage their data more freely and servers limit auto-generated data. You can still easily auto-increment a field in a NoSQL database by using a counter collection on the _id
field. In this tutorial, we will learn the steps to auto-increment a field in MongoDB Atlas, MongoDB’s Database-as-a-Service platform.
Table of Contents
This tutorial assumes that you have installed and configured a MongoDB Atlas account and cluster. If you have not done so, follow the steps below:
Note: All the commands in this tutorial are run on the mongo shell.
To ensure your database remains scalable with a large number of documents, we recommend avoiding the use of an auto-increment pattern for the _id field or any field in MongoDB. In case of a concurrency failure, the use of such a pattern can result in data duplication.
The default value ObjectId is ideal for the auto-generated _id rather than an auto-incremented value. Consult MongoDB’s official documentation on Object Id for more information.
Although MongoDB does not support auto-increment sequence as a default feature like some relational databases, we can still achieve this functionality using a counter collection. The counter collection will have a single document that tracks the current unique identifier value. Learn how to create a counter collection and implement auto-increment below.
We will implement auto-increment functionality using Triggers in MongoDB Atlas. Triggers allow you to schedule the execution of server-side logic or execute it in response to database events. Learn more about what database triggers are.
For this approach, you need at least two collections.
The first collection will hold the details of students; hence we will name it students. Run the following command to create a students collection.
db.createCollection("students");
The second collection will hold the current unique identifier value for the students. Let’s name it counters. Run the following command to create a counters collection.
db.createCollection("counters");
To implement triggers for auto-increment, log into your MongoDB Atlas account, open the cluster you want to work on. and click on Triggers.
Click on Add Trigger and set the following values to the corresponding fields.
_
Increment_
Trigger In the Function field, add the following code.
exports = async function(changeEvent) {
var docId = changeEvent.fullDocument._id;
const countercollection = context.services.get("<ATLAS-CLUSTER>").db(changeEvent.ns.db).collection("counters");
const studentcollection = context.services.get("<ATLAS-CLUSTER>").db(changeEvent.ns.db).collection(changeEvent.ns.coll);
var counter = await countercollection.findOneAndUpdate({_id: changeEvent.ns },{ $inc: { seq_value: 1 }}, { returnNewDocument: true, upsert : true});
var updateRes = await studentcollection.updateOne({_id : docId},{ $set : {studentId : counter.seq_value}});
console.log(`Updated ${JSON.stringify(changeEvent.ns)} with counter ${counter.seq_value} result : ${JSON.stringify(updateRes)}`);
};
<ATLAS-CLUSTER>
with your cluster service name.Your code should look like this.
Click Save to save the trigger.
In the above code, we have first initialized the counters collection and defined an insert event trigger for the students collection. When you insert a document into the students collection, the trigger function executes, which updates the seq_value
field in the counters collection and adds the seq_value
to the students collection as studentId
field.
The mongodb inc operator increments a field by the specified value (1 in our case). The returnNewDocument parameter, when set to true, atomically returns an incremented number, while the upsert parameter, when set to true, adds a new studentId
counter if the current namespace doesn’t have it.
To see the trigger in action, run the following commands to insert a few documents into the students collection.
db.students.insert({
"name":"Jhon Doe"
});
db.students.insert({
"name":"David Smith"
});
db.students.insert({
"name":"Amanda Herny"
});
Run the following code to ensure the results are consistent.
db.students.find().pretty();
Once the code execution is complete, you will get the following results.
{
"_id" : ObjectId("60b3a7fa1ce57d7f961a2c12"),
"name" : "Jhon Doe",
"studentId" : NumberLong(1)
}
{
"_id" : ObjectId("60b3a8021ce57d7f961a2c13"),
"name" : "David Smith",
"studentId" : NumberLong(2)
}
{
"_id" : ObjectId("60b3a8091ce57d7f961a2c14"),
"name" : "Amanda Herny",
"studentId" : NumberLong(3)
}
You can also view the results graphically in MongoDB Atlas by traversing to the students collection.
To check the document in the counters collection, run the following code.
db.counters.find().pretty();
You will get the following results.
{ "_id" : ObjectId("60b39cf75886657cb75527bb"), "seq_value" : "0" }
{
"_id" : {
"db" : "myFirstDatabase",
"coll" : "students"
},
"seq_value" : NumberLong(3)
}
Notice the value of seq_value field in the counters collection. It has increased to 3 from 0, as we have inserted 3 documents into the students collection.
Though auto-incrementing in MongoDB isn’t a standard feature, you can still obtain a similar result by implementing a counter collection on the _id field. You just need to maintain a different collection and document for tracking the counts. The biggest advantage of this approach is that it is driver agnostic, meaning any client inserting the documents fires the trigger to set the correct counter value. If you want to learn about more approaches, read our blog on generating globally unique identifiers for MongoDB.
You can implement auto-increment in MongoDB by using a counter collection, findOneAndUpdate with $inc, and a JavaScript function.