Triggers Treats and Tricks - Auto-Increment a Running ID Field
Rate this article
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.
- 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
studentscollection example, we would like to get the
studentIdfield auto incremented.
I wanted to share an interesting solution based on triggers, and throughout this article, we will use a students collection example with
studentsIdfield to explain the discussed approach.
First, verify that you have an Atlas project with owner privileges to create triggers.
Three main components allow our auto-increment mechanism to function.
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:
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
counterscollection document that stores the current counter per collection. It uses
findOneAndUpdateto 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
<COLLECTION_NAME>Id(in this example,
Important: Replace <ATLAS-SERVICE> 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
upsert: truein case this is the first document.
countercollection document after the first run on our student collection will look like this:
Now let's define our trigger based on our Atlas cluster service and our database and source collection, in my case,
Please make sure to select "Event Ordering" toggled to "ON" and the "insert" operation.
Now let's associate it with our pre-built function:
Once we will insert our document into the collection, it will be automatically updated with a running unique number for
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.