MongoDB Data Change

How to listen for changes to a MongoDB collection.

Rio Weber
riow
3 min readFeb 7, 2018

--

So you’re doing something real-time?

Introducing MongoDB Change Streams

Change Streams are used in Database Mirroring

Database Mirroring: the replication of redundant copies of a database. The purpose is to ensure continuous data availability and minimize or avoid downtime.

Replication: a feature allowing multiple database servers to share the same data, thereby ensuring redundancy and facilitating load balancing. See Replication.

How it works:

  1. replication creates additional copies of the data and allows for automatic fail-over to another node. Replication can also help with horizontal scaling.

This architecture in Mongo is also called a Replication Set.

You can then also make a replica set part of a sharded cluster.
— That’s for another post.

Replication Sets

Replica Set: A cluster of MongoDB servers that implements master-slave replication and automated failover.
More: https://docs.mongodb.com/manual/replication/

  • For this example there will only be a master.

To use changeStreams the Mongo Database must be a Replication Set.

By default your Database will be a “Standalone” database.

Standalone: An instance of mongod that is running as a single server and not as part of a replica set.

In order to use changeStreams you must convert your “Standalone” database into a “Replica Set” database.

How to Convert a Standalone to a Replica Set.

1. Shut down the Standalone mongod instance.

2. Restart the instance as a Replica Set.
Use the --replSet option to specify the name of the new replica set.
The following command starts a standalone instance as a member of a new replica set named
rs0.

All options:

3. Connect to the mongodb with a mongo shell.

4. Initiate the new replica set.

About rs.initiate() command: https://docs.mongodb.com/manual/reference/method/rs.initiate/#rs.initiate

You should get the following, you can now exit mongo.

Your database is now a member of a Replica Set!

You can check the status of the Replica Set with rs.status()

How To Use

The following example is a practical application for how you might use this.

LightBase

The following snippet is taken from the LightBase project;
a full working server Framework using everything posted here and more, check it out here.

Example:

When the Mongo Collection User changes in any way, all users are resent over the Socket.io connection.

https://gist.github.com/riodw/f1cdf875ee46e093e92ad917140eff95

Using: Node.js, MongoDB, Mongoose.js, Express.js, Socket.io

How It Works

First is to put a watch on the Mongoose Model

We can then use the event “Change” to get notified anytime the Collection changes.

Now every time the database changes we can query for all the data and resend it.

NOTE: you must start the mongodb with the --replSet rs0 everytime you start the database, otherwise it will default to a standalone.

./END

--

--