How to Setup Replicas using MongoDB - Part 2

Have a Database Problem? Speak with an Expert for Free
Get Started >>


In this two-piece article we are showing step by step how to setup replicas in MongoDB. You might not need replication for development but if you ever want to move your application to a production environment you will need replication. Loss of data is unacceptable by today’s standards. In this article we are setting a replica set up on a local environment for simplicity and ease of understanding. In Part 1 of this tutorial we successfully completed the setup for a replication set in MongoDB and now we need to verify that the data is actually being replicated. If you just jumped straight into Part 2 we highly recommend you read Part 1 first even if you just skim it for context. Now let’s jump into verifying that our replication set is working as expected.

Verify Replication

To verify replication let’s get into the mongo shell of the PRIMARY instance (27000) and create a piece of data and make sure it gets replicated.

Access the mongo shell of the PRIMARY instance:

mongo localhost:27000

Add a document to a collection:

>db.products.insert({product: "Soy Milk", quantity: 100, price: 2.99})

This can be any collection or document you want. We created a collection named products with a document in it with a product name, a quantity, and a price.

Now let’s see if this document has been replicated by getting into the mongo shell of any SECONDARY instance, we’ll use the SECONDARY instance on port 27001. You can also use the SECONDARY instance on port 27002 if you prefer or even try it on both.

mongo localhost:27001

In order for mongo to allow us to query on a SECONDARY instance we’ll need first to allow it by executing this command:


Why do we need to do this? Why can’t we just query the second instance? Well, the default for SECONDARY instances in a replica set is to NOT allow querying. Replicas aren’t instantaneously synced and so your SECONDARY instances might be slightly behind the PRIMARY sets data but in certain cases it is fine to allow this. Now that we’ve executed this command we can query the SECONDARY instance like normal.

Now let’s execute a query for the data we created on the PRIMARY instance:

        "_id" : ObjectId("5cf54035ac2fa33ba9ac445b"),
        "product" : "Soy Milk",
        "quantity" : 100,
        "price" : 2.99

We queried for all the documents in the products collection and got back the document we created on our PRIMARY instance!


In Part 1 of this article we showed you how to setup a replication set on a local environment with MongoDB. We used three instances and were able to use the rs.status() command to check that our replication set was what we expected.

In Part 2 we wanted to verify with our own eyes that data inserted on the primary instance was in fact replicated on a secondary instance. What we’ve gone through here are the basics of setting up a replication set but once you’ve setup your first replication set further setups become a little bit easier and less intimidating.

If you need help setting up replication in MongoDB don’t hesitate to reach out to the database experts at Object Rocket. The biggest way we can help you is buy letting us solve all the problems that come with managing your data, especially those complex problems that arise in production environments. Thanks for joining us for this tutorial and don’t hesitate to reach out to us for help.

How to Setup Replicas using MongoDB Part 1

How to Setup Replicas using MongoDB Part 2

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.