Reading from secondary database in MongoDB replication

Steps For Reading From Secondary Database in MongoDB replication

1) find the current primary
2) connect to primary and create a collection
3) Connect to secondary and verify contents of the collection

Find the current primary

$ mongo --host mongoserver2 --port 20051
mongo > rs.status()

connect to primary and create a collection

$ mongo --host mongoserver2 --port 20051
mongo > show dbs
mongo > use school
mongo > db.student.insertOne(
  { "Class": "A",
    no_students: 20

Reading from secondary

$ mongo -nodb
> conn2 = new Mongo("mongoserver1:20050")
> conn2.setSlaveOk()
> conn2.adminCommand( { listDatabases: 1 } )
> secondaryDB = conn2.getDB("school")
> secondaryDB.runCommand({ listCollections: 1})
> secondaryDB.student.find()

Watch the Video

[vdo id=e0ff667137e34c43be133c689e1296fa]

Leave a comment

Your email address will not be published. Required fields are marked *

We assure our best to meet up to your expectations

Your Feedback will help us

Glad That You Liked It

Your Feedback will help us to improve