How to execute mongo commands through shell

Lets say you need to find out which is the primary node in the mongo cluster in your shell script,

mongo –host –username=userid –password=secret –eval “db.runCommand(‘ismaster’).primary”

This would quickly open the mongo shell and execute the commands, and give the output as the last line

MongoDB shell version v4.0.5
connecting to: mongodb://
Implicit session: session { “id” : UUID(“65f2e5ab-aa97-4306-8a20-afd70a4ceeb0”) }
MongoDB server version: 4.0.5

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.