MongoDB Realm functions

Aniruddha Amit Dutta
4 min readJul 4, 2021

In this article we will create two endpoints for an API to insert and fetch a document in MongoDB Atlas using while trying to explore MongoDB Realm functions. These are serverless functions which are easy to deploy on Realm platform.

Create a project folder and run the following commands -

$ cd project
$ npm init
$ npm install --save mongodb
$ npm install --save mongodb-stitch-server-sdk
$ npm i -g vercel
$ npm install --save dotenv

We will create a cluster in MongoDB Atlas using the following documentation-

Inside this cluster we will create a database (remember its name) (here it is clubhub_data) and then add an events collection ( ignore other collections which are present in below image )-

For allowing access to this database to everyone go to -
Network Security -> add IP address -> allow access from anywhere

After certain time this entry status will become active from pending. We will now add API Key Authentication to connect to Realm by following steps -
Go to Realm -> Authentication -> API Keys -> create API Key

create the following file to store this key
project/.env

MONGO_API_KEY=<Your key here>
EVENTS_COLLECTION=events
CLUBHUB_DB=clubhub_data
STITCH_APP_NAME=<Your APP ID>

You could see your APP ID listed when you will click on Realm like image below -

A Database consists of collections and a collection consists of documents . We will now create a file to which will help us to insert some event document in our collection and then to display it.

project/api/insertEvent.js

Now run the following command in project folder-

$ vercel dev

Add .env entry inside .gitignore file which is automatically generated when we run $ vercel dev command. If you are facing ENOSPC error run the following command -

$ sudo sysctl -w fs.inotify.max_user_watches=524288

Run the vercel dev command again and go the following url in your browser-

http://localhost:3000/api/insertEvent.js

You will see something like the following output (I have highlighted the event that we have inserted in the image) (there are also other events present which were inserted for testing )-

Finally we will move towards using Realm functions . We will now create a function to fetch this event event that we have inserted in our database using following steps Go To-

Realm -> Functions -> create new function

Inside the settings choose Application authentication and in the function editor paste the following code .

Click on save draft -> review draft and deploy -> deploy
Now our realm serverless function is deployed and ready to be used. For testing this function create the following file -

project/api/getEvent.js

For viewing the output we will again run $ vercel dev command and open the following url in our browser -

http://localhost:3000/api/getEvent.js

output will look something like this -

Now we have created two endpoints in our API to create and fetch the event.

I have posted the code on Github for reference

https://github.com/duttaANI/MongoDB_Realm

Errors encountered -
I encountered some error while running $ vercel dev command and the following link helped to resolve it -

https://stackoverflow.com/questions/65300153/error-enospc-system-limit-for-number-of-file-watchers-reached-angular/65347277#65347277

I hope this article was helpful in giving an introduction to MongoDB realm . Any suggestions to improve this article are welcome.

--

--

Aniruddha Amit Dutta

Software Engineer @Tejas Networks | Writer | Cloud and DevOps Enthusiast