1. Install MongoDB command line utilities
  2. Dump your existing database
  3. Create a new folder for the Mongo database to store its files
  4. Backup MongoDB configuration file
  5. Edit the MongoDB configuration file
  6. Restart MongoDB
  7. Activate the new Replica Set
  8. Confirm that your MongoDB Replica Set is using the WiredTiger storage engine
  9. Restore database
  10. Delete database dump
  11. Sources / Credits

If you want to take advantage of MongoDB Change Streams (which will allow an event to be fired whenever a collection is modified instead of polling the database for changes), you need to change your MongoDB instance to a Replica Set. Here is the full process using the Terminal.

I am using a local installation of MongoDB 3.6 installed via Homebrew on macOS, but this procedure also works for practically any version, provided that you edit a couple of the commands that I show in the guide.

Install MongoDB command line utilities

Homebrew provides a formula for installing the MongoDB command line utilities that is not installed with the server by default.

1
2
3
brew tap mongodb/brew
brew install mongodb-database-tools
brew install mongodb-community-shell

Dump your existing database

1
mongodump --out ~/mongobackup

Create a new folder for the Mongo database to store its files

1
mkdir -p /usr/local/var/repl-emagine-data-wiredtiger

Backup MongoDB configuration file

Now, make a backup of your existing mongodb configuration and create a new mongodb configuration.

1
cp /usr/local/etc/mongod.conf /usr/local/etc/mongod.conf.bak

Edit the MongoDB configuration file

Edit the configuration and modify it to use wiredTiger as the storage engine. This handles issues you may later have with read/write concerns, where some operations require specific read/write concerns (e.g. majority) and will throw errors if your storage engine does not support them. The recommended engine is thus wiredTiger.

1
vim /usr/local/etc/mongod.conf

Pay attention to the storage.dbPath path of the configuration file. This value should match the folder path that you created before.

1
2
3
4
5
6
7
8
9
10
11
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  engine: wiredTiger
  dbPath: /usr/local/var/repl-emagine-data-wiredtiger
net:
  bindIp: 127.0.0.1
replication:
  replSetName: replocal

Restart MongoDB

Restart the database. Edit the command appropriately if using a different version of MongoDB.

1
brew services restart mongodb-community@3.6

Activate the new Replica Set

Access the new MongoDB instance via the command line:

1
mongo

Initiate a new replica set with replocal as the _id.

1
2
rs.initiate({_id: "replocal", members: [{_id: 0, host: "127.0.0.1:27017"}] })
{ "ok" : 1 }

It will tell you that you are secondary, but you will soon be promoted to primary:

1
replocal:PRIMARY> rs.status()

Confirm that your MongoDB Replica Set is using the WiredTiger storage engine

Run in mongo CLI:

1
replocal:PRIMARY> db.serverStatus().storageEngine

You should get this:

1
2
3
4
5
6
{
	"name" : "wiredTiger",
	"supportsCommittedReads" : true,
	"readOnly" : false,
	"persistent" : true
}

Exit the CLI

1
replocal:PRIMARY> exit

Restore database

Restore your database from the dump that you created before.

1
mongorestore ~/mongobackup

Go for a looooong coffee ☕️ and come back.

Delete database dump

Do not forget to delete the dump after you are sure that everything is correctly restored.

1
rm -rf ~/mongobackup

Sources / Credits

  1. “Setup MongoDB on localhost as Replica Set”
  2. “Change Standalone to WiredTiger”