mongodbrustconnectionclientreplicaset

When connecting a Rust based mongodb client (rust mongodb 2.6.0) to a replica set, Is there a way to ignore a failure from one of the hosts?


I'm working on a rust application that reads a mongodb document. The connection is done using a string that should represent a replica set: "mongodb://host1:port1,host2:port2...".

I use similar code to the following:

    let servers_list = "mongodb://host1:port1,host2:port2,host3:port3";
    let mut client_options = ClientOptions::parse(servers_list).await?;
    servers_list.app_name - Some("name".to_string());
    let client = Client::with_options(client_options)?;
    let db = client.database("database_name".to_string())?;
    let collection = db.collection::<Document>("collection_name".to_string());

Given that only host1:port1 is valid, my read attempt crashes with a returned Result Err. I wish to read from just one of the servers, without checking all of them for validity.

When connecting to the same replica set using Compass, I get a connection to the valid host, and its just like trying to connect to that one without mentioning the others.

So my question is: Can I get the same behavior using the rust library without any special error handling on my side? Is it possible to use the options to get that behavior?

Thanks


Solution

  • The issue turned out to be a bad server configuration, which caused the mongodb servers to just be 3 independent servers, and when the client failed to obtain one of the connections it returned the error.