Direct connections from replica set client

I’m writing an internal tool to wrap some of the common things we do to our MongoDB replica sets, and as part of that I occasionally find myself wanting to create direct connections to individual servers in the replica set (for example to call the serverStatus command or getParameter command).

At the moment I do this by creating extra Clients in addition to the “main” replica-set client, but I was wondering if there’s some way I can use the “main” client to direct commands to a given member?

I think I can do this by assigning a tag to each member with its hostname, but I was wondering if there’s already some built in way of doing this that doesn’t involve configuring the replica set some special way.

I’d love to be able to do something like this:

var client *mongo.Client
client, _ = mongo.Connect(
    ctx,
    options.Client().ApplyURI(
        "mongodb://repl-1,repl-2,repl-3/?replicaSet=my-repl"
    )

result := client.Database("admin").
    RunCommand(
        ctx,
        bson.D{{Key: "serverStatus", Value: 1}},
        options.RunCmd().Member("repl-1")
    )
// do something with result

Hi @Sarah_Hodne and welcome in the MongoDB Community :muscle: !

I think what you are trying to do is here:

You need to tag the members and then use the readPreference option to target the member of your choice with your read operation. I hope this also works for the admin commands you are trying to send.

Cheers,
Maxime.