1.

Multiple addresses are stored as an embedded document for a user. Within the UI, each of these addresses need to be shown as an individual document along with all the user details. Should this be done programmatically? Or is there a simpler way to achieve this in MongoDB?

Answer»

Suppose we have 3 servers abc.com,xyz.com andpqr.com.

  • First, we need to start MongoD on each of the servers with appropriate options.
    • mongod --replSet "rs0" --bind_ip abc.com –port 27017
    • mongod --replSet "rs0" --bind_ip xyz.com --port 27017
    • mongod --replSet "rs0" --bind_ip pqr.com –port 27017

option –replset is used for creating a replica set. We have given a replica set NAME as rs0. Bind IP is the IP to which SERVER can be connected to from outside.

  • Connect to any of the servers and connect to mongo shell.

Login to server abc.com and run command 

mongo>

it will take you to mongo shell.

Now we need to initiate the replica set with a configuration of all 3 MEMBERS.

rs.initiate( {    _id : "rs0",    members: [       { _id: 0, host: "abc.com:27017" },       { _id: 1, host: "xyz.com:27017" },       { _id: 2, host: "pqr.com:27017" } })

MongoDB initiates a replica set, using the default replica set configuration.

  • To view the configuration of the replica set from any member we can run a command 

rs.conf()

Also to check the status for each member we can run command 

rs.status()

The server from which we run rs.initiate will become primary and other 2 servers will become secondary.



Discussion

No Comment Found