When does the replication from primary to secondary happen in mongodb

mongodb

I have setup a 3 node replica set.

Writing to primary node works but when I read with readPreference=secondary; I get values as null.

When I open the local node with robomongo I see that the collection does not exist. So it has not been replicated?

I am not sure when it gets replicated or am I missing something?

I have used the following URI

'mongodb://DNS1,DNS2,DNS3/?readPreference=secondary&w=0'

Update

Here is how my rs.status looks like

rs0:PRIMARY> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2014-03-10T10:57:13Z"),
    "myState" : 1,
    "members" : [
            {
                    "_id" : 0,
                    "name" : "NWI47:27017",
                    "health" : 1,
                    "state" : 1,
                    "stateStr" : "PRIMARY",
                    "uptime" : 16593,
                    "optime" : Timestamp(1394432703, 1),
                    "optimeDate" : ISODate("2014-03-10T06:25:03Z"),
                    "self" : true
            },
            {
                    "_id" : 1,
                    "name" : "t-plat-mongodb1.paand.local:27017",
                    "health" : 1,
                    "state" : 2,
                    "stateStr" : "SECONDARY",
                    "uptime" : 1656,
                    "optime" : Timestamp(1394432703, 1),
                    "optimeDate" : ISODate("2014-03-10T06:25:03Z"),
                    "lastHeartbeat" : ISODate("2014-03-10T10:57:13Z"),
                    "lastHeartbeatRecv" : ISODate("2014-03-10T10:57:11Z"),
                    "pingMs" : 187,
                    "syncingTo" : "NWI47:27017"
            },
            {
                    "_id" : 2,
                    "name" : "NWI27.np.i:27017",
                    "health" : 1,
                    "state" : 2,
                    "stateStr" : "SECONDARY",
                    "uptime" : 1664,
                    "optime" : Timestamp(1394432703, 1),
                    "optimeDate" : ISODate("2014-03-10T06:25:03Z"),
                    "lastHeartbeat" : ISODate("2014-03-10T10:57:11Z"),
                    "lastHeartbeatRecv" : ISODate("2014-03-10T10:57:12Z"),
                    "pingMs" : 102,
                    "syncingTo" : "NWI47:27017"
            }
    ],
    "ok" : 1

}

Best Answer

Mongo syncs instantly, so there is something wrong with your Replica set.

MongoDB replica sets are something that you need to get right from when they are first set up. If they aren't set up correctly, they can be difficult to fix.

Configuration of the Replica Set should (normally) be done from the master only. If your Set isn't live yet, the best option may be to re-create it.

Also, not sure what robomongo is, but you're probably better off using the native mongo client to find out what is going on.

The rs.status() command should give you output like this

rs0:SECONDARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2014-03-10T10:42:27Z"),
"myState" : 2,
"syncingTo" : "mongo-master:27017",
"members" : [
    {
        "_id" : 0,
        "name" : "mongo-master:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 3008469,
        "optime" : Timestamp(1394448146, 1),
        "optimeDate" : ISODate("2014-03-10T10:42:26Z"),
        "lastHeartbeat" : ISODate("2014-03-10T10:42:26Z"),
        "lastHeartbeatRecv" : ISODate("2014-03-10T10:42:26Z"),
        "pingMs" : 1
    },
    {
        "_id" : 3,
        "name" : "mongo-slave3:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 3012206,
        "optime" : Timestamp(1394448146, 1),
        "optimeDate" : ISODate("2014-03-10T10:42:26Z"),
        "self" : true
    },
    {
        "_id" : 4,
        "name" : "mongo-slave4:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 890533,
        "optime" : Timestamp(1394448146, 1),
        "optimeDate" : ISODate("2014-03-10T10:42:26Z"),
        "lastHeartbeat" : ISODate("2014-03-10T10:42:26Z"),
        "lastHeartbeatRecv" : ISODate("2014-03-10T10:42:26Z"),
        "pingMs" : 0,
        "syncingTo" : "mongo-master:27017"
    }
],
"ok" : 1
}