Search code examples
mongodbmongodb-.net-driver

C# MongoDb Connect to Replica Set Issue


According to the mongodb website, I should be able to connect to a replica set if I just give it one member from the replica set:

"The C# Driver is able to connect to a replica set even if the seed list is incomplete. It will find the primary server even if it is not in the seed list as long as at least one of the servers in the seed list responds (the response will contain the full replica set and the name of the current primary)." http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Connectionstrings

However, I cannot get my driver to connect if I just give it a secondary member.

This is my current connection statement:

m_server = MongoServer.Create(new MongoServerSettings { ConnectionMode = ConnectionMode.ReplicaSet, Server = new MongoServerAddress(connection) });

The 'connection' variable is: mongodb://servername/?safe=true

I saw this: https://jira.mongodb.org/browse/CSHARP-500, and I did run rs.status(), and did use the correct server name. Any help is appreciated!


Solution

  • So, the connection variable is a full connection string, not something to pass to MongoServerAddress. Also, you can specify the connection mode on the connection string as well. Try this:

    connection = "mongodb://servername/?safe=true&connect=replicaset";
    m_server = MongoServer.Create(connectionString);