Search code examples
mongodbmongo-shellreplicaset

MongoDB run commands on a specific secondary node


I want to db.runCommand on a secondary node of a replica set, but not the primary (example).

What connection string should I use in mongosh to connect to it?

Connecting to the replica set (e.g. mongosh mongodb+srv://example.mongodb.net/) points me to the primary.


Solution

  • a direct connection is required, i.e.

    mongosh "mongodb://example-shard-00-02.web.mongodb.net:27017" \
       --tls \
       --authenticationDatabase admin \
       --username <user> --password <pwd>