1. 程式人生 > >Using MongoDB as realtime DB with nodeJS.

Using MongoDB as realtime DB with nodeJS.

Have you ever run into a scenario where you wanted to update your UI as soon as there is some change to your Mongo database ? For example, A new user gets added and you wanted that change to reflect in you UI without having to make an API call or constantly polling for changes. If so, then this post is for you.

While the go to would be to use real time databases like firebase or RethinkDB, you actually can achieve this using your MongoDB too and it is pretty simple. MongoDB has something called that allows you to listen to your database.

The method I’m going to show uses  , you can also achieve the same using (click those links to read more about replicasets and sharded clusters)

  • So, the first step would be to convert your stand alone MongoDB to replica sets. This is because is not available with standalone MongoDB (Honestly I was about to give up on seeing that, but trust me it’s simple).
$ mongod --port 27017 --replSet rs0

PS: Stop your current standalone mongo and then run the above command.

The above command starts your mongo instance as a named as rs0.

  • Now with your mongo running as before creating a new database we must initiate our replication set. To do so, open a new window and do the following
$ mongo$ rs.initiate() 
  • Now let’s go and create our database. I use Robo 3T to manage my databases, you can create it in terminal using $use <Database_name> . I’m creating a db called UserDB.
Create a new Database
  • Now our DB part is set. Scaffold an Express application and install Mongoose. My User model looks like this,
User model
  • Now assume I have two users inside my User collection of UserDB. Now I want to subscribe to my UserDB for any changes on User collection. In your app.js code, the mongoDB connection string should look like this,
require('./models/connection').connectMongoDB('mongodb://localhost/UserDB?replicaSet=rs0');

I’m passing my dbURL to my connection module. (we need to explicitly mention the replicaSet).

  • Now you can subscribe to you userDB using changeStreams. In my case I listen inside web socket connection, so as to send those changes immediately on to the front end. Something like below,

I listen to the changes in User collection using the watch() method, and whenever there is any change, the change event listener provided by changeStreams gets fired. It accepts a callback function that receives the changed data as parameter.

We can go ahead and test it to see if it works. Run an update operation onUser collection and at the same time in another tab watch for the console.log messages of your app. You can see the changes real time. At this place, We can now forward this to the front end using

You’re mongoDB is now acting as a real time DB ?.