One liner to find a shard in a MongoDB cluster

I occasionally post so called "one liners", shell commands that can be used to filter out some data I need. The main reason I do this is that I can later find this when I try to google for it. This will be my first one liner for MongoDB. Ok, so it is actually 3 related one liners.

If you need to find out if a shard is already part of your MongoDB cluster, try this:


MONGOS="ec2-54-228-106-184.eu-west-1.compute.amazonaws.com"
echo "db.shards.find()" | mongo $MONGOS/config | grep Shard4 | wc -l

The result will be either 1 or 0.

Now, if the shard exists, you might want to know a hostname and port number of one of the members of that replicaset:


echo 'db.shards.find( { "_id" : "Shard4" }, { "host" : true, "_id" : false } )' | mongo $MONGOS/config | grep Shard4 | awk -F":" '{ print $2}' | awk -F"/" '{print $2}'
ec2-54-246-27-205.eu-west-1.compute.amazonaws.com


echo 'db.shards.find( { "_id" : "Shard4" }, { "host" : true, "_id" : false } )' | mongo $MONGOS/config | grep Shard4 | awk -F":" '{ print $3}' | awk -F"," '{print $1}'
27017

This is useful when you try to deploy MongoDB clusters with some automated tooling or AWS autoscaling.

About the bookAbout this siteAcademicAccordAmazonAppleBeginnersBooksBuildBotBusiness modelsbzrCassandraCloudcloud computingclsCommunitycommunityleadershipsummitConsistencycoodiaryCopyrightCreative CommonscssDatabasesdataminingDatastaxDevOpsDistributed ConsensusDrizzleDrupalEconomyelectronEthicsEurovisionFacebookFrosconFunnyGaleraGISgithubGnomeGovernanceHandlerSocketHigh AvailabilityimpressionistimpressjsInkscapeInternetJavaScriptjsonKDEKubuntuLicensingLinuxMaidanMaker cultureMariaDBmarkdownMEAN stackMepSQLMicrosoftMobileMongoDBMontyProgramMusicMySQLMySQL ClusterNerdsNodeNoSQLNyrkiöodbaOpen ContentOpen SourceOpenSQLCampOracleOSConPAMPParkinsonPatentsPerconaperformancePersonalPhilosophyPHPPiratesPlanetDrupalPoliticsPostgreSQLPresalespresentationsPress releasesProgrammingRed HatReplicationSeveralninesSillySkySQLSolonStartupsSunSybaseSymbiansysbenchtalksTechnicalTechnologyThe making ofTransactionsTungstenTwitterUbuntuvolcanoWeb2.0WikipediaWork from HomexmlYouTube

Search

Recent blog posts

Recent comments