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 Client
s 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