# Triggers Treats and Tricks - Auto-Increment a Running ID Field

In this blog series, we are trying to inspire you with some reactive Realm trigger use cases. We hope these will help you bring your application pipelines to the next level.

Essentially, triggers are components in our Atlas projects/Realm apps that allow a user to define a custom function to be invoked on a specific event.

-   **Database triggers:** We have triggers that can be scheduled based on database events—like `deletes`, `inserts`, `updates`, and `replaces`—called database triggers.
-   **Scheduled triggers:** We can schedule a trigger based on a `cron` expression via scheduled triggers.
-   **Authentication triggers:** These triggers are only relevant for Realm authentication. They are triggered by one of the Realm auth providers' authentication events and can be configured only via a Realm application.

For this blog post, I would like to showcase an auto-increment of a running ID in a collection similar to relational database sequence use. A sequence in relational databases like Oracle or SqlServer lets you use it to maintain a running ID for your table rows.

If we translate this into a `students` collection example, we would like to get the `studentId` field auto incremented.

``` javascript
{
  studentId : 1,
  studentName : "Mark Olsen",
  age : 15,
  phone : "+1234546789",
},
{
  studentId : 2,
  studentName : "Peter Parker",
  age : 17,
  phone : "+1234546788",
}
```

I wanted to share an interesting solution based on triggers, and throughout this article, we will use a students collection example with `studentsId` field to explain the discussed approach.

## Prerequisites

First, verify that you have an Atlas project with owner privileges to create triggers.

-   MongoDB Atlas account, Atlas cluster
-   A MongoDB Realm application or access to MongoDB Atlas triggers.

> If you haven't yet set up your free cluster on MongoDB Atlas, now is a great time to do so. You have all the instructions in this blog post.
> 
## The Idea Behind the Main Mechanism

Three main components allow our auto-increment mechanism to function.

## 1. Define a Source Collection

We should pick the collection that we need the auto-increment to work upon (`students`) and we can define a unique index on that field. This is not a must but it makes sense:

``` javascript
db.students.createIndex({studentsId : 1}, {unique : true});
```

## 2. Define a Generic Function to Auto-Increment the ID

In order for us to reuse the auto-increment code for more than one collection, I've decided to build a generic function and later associate it with the relevant triggers. Let's call the function `autoIncrement`. This function will receive an "insert" event from the source collection and increment a helper `counters` collection document that stores the current counter per collection. It uses `findOneAndUpdate` to return an automatically incremented value per the relevant source namespace, using the \_id as the namespace identifier. Once retrieved, the source collection is being set with a generic field called `Id` (in this example, `studentsId`).

```javascript
exports = async function(changeEvent) {
    // Source document _id
    const docId = changeEvent.fullDocument._id;

    // Get counter and source collection instances
    const counterCollection = context.services.get("").db(changeEvent.ns.db).collection("counters");
    const targetCollection = context.services.get("").db(changeEvent.ns.db).collection(changeEvent.ns.coll);

    // automically increment and retrieve a sequence relevant to the current namespace (db.collection)
    const counter = await counterCollection.findOneAndUpdate({_id: changeEvent.ns },{ $inc: { seq_value: 1 }}, { returnNewDocument: true, upsert : true});

    // Set a generic field Id 
    const doc = {};
    doc`${changeEvent.ns.coll}Id`] = counter.seq_value;
    const updateRes = await targetCollection.updateOne({_id: docId},{ $set: doc});

    console.log(`Updated ${JSON.stringify(changeEvent.ns)} with counter ${counter.seq_value} result: ${JSON.stringify(updateRes)}`);
};
```

>Important: Replace \ with your linked service. The default value is "mongodb-atlas" if you have only one cluster linked to your Realm application.

Note that when we query and increment the counter, we expect to get the new version of the document `returnNewDocument: true` and `upsert: true` in case this is the first document.

The `counter` collection document after the first run on our student collection will look like this:

``` javascript
{
  _id: {
       db: "app",
       coll: "students"
  },
  seq_value: 1
}
```

## 3. Building the Trigger on Insert Operation and Associating it with Our Generic Function

Now let's define our trigger based on our Atlas cluster service and our database and source collection, in my case, `app.students`.

Please make sure to select "Event Ordering" toggled to "ON" and the "insert" operation.

Now let's associate it with our pre-built function: `autoIncrement`.

Once we will insert our document into the collection, it will be automatically updated with a running unique number for `studentsId`.

## Wrap Up

With the presented technique, we can leverage triggers to auto-increment and populate id fields. This may open your mind to other ideas to design your next flows on MongoDB Realm.

In the following article in this series, we will use triggers to auto-translate our documents and benefit from Atlas Search's multilingual abilities.

> If you have questions, please head to our [developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.