Gustavo Bazan

1 result

Using MongoDB CLI to Get Atlas Performance Advice From Your Terminal

MongoDB CLI brings the power of your MongoDB cloud (MongoDB Atlas, MongoDB Cloud Manager, and MongoDB Ops Manager) to your terminal, helping you write advanced scripts and automate different workflows for your MongoDB cloud infrastructure. We released MongoDB CLI's first stable version back in June. So far we've seen more than 100 clusters deployed with the tool, as well as with many other operations such as getting metrics for these clusters or managing backups. Meanwhile, we've been busy adding new features and listening to your feedback as to what we should add next. With the latest release of mongocli , we have added the ability to get Atlas performance recommendations. Let's see how they work. $ mongocli atlas clusters create slowQueriesDemo \ --region EASTERN_US \ --members 3 \ --tier M30 \ --provider GCP \ --mdbVersion 4.4 \ --diskSizeGB 30 This command deploys a three-member replica named slowQueriesDemo, with GCP as the backing cloud provider. You can always use mongocli atlas clusters create --help to review all available options when creating a cluster. Getting your new cluster ready can take some time. Use mongocli to be notified when your changes are available with a watch : $ mongocli atlas clusters watch slowQueriesDemo Performance Advisor works by scanning your Atlas Cluster logs and finding any slow operations that could be affecting the performance of your queries. For this example, I loaded my demo Atlas cluster with some dummy data. I executed a series on MongoDB commands where I expect to see the Performance Advisor recommend new indexes to improve my queries. If you're not seeing any suggestions, please check our docs . The performance advisor command requires a process identifier. To check for the processes available to our project: $ mongocli atlas processes ls ID REPLICA SET NAME SHARD NAME VERSION atlas-00-00.gcp.mongodb.net:27017 atlas-fjszq1-shard-0 4.4.1 atlas-00-01.gcp.mongodb.net:27017 atlas-fjszq1-shard-0 4.4.1 atlas-00-02.gcp.mongodb.net:27017 atlas-fjszq1-shard-0 4.4.1 We want to pick one process ID related to our cluster, and then use it with the performance advisor: $ mongocli atlas performanceAdvisor suggestedIndexes ls --processName atlas-00-01.gcp.mongodb.net:27017 ID NAMESPACE SUGGESTED INDEX 5f75c6d38788f14f816f80ee loadTest.saildrone { timeUTC: 1 } 5f75c6d38788f14f816f80ef loadTest.saildrone { id: 1, arrayOperation: 1, decimal: 1, nested.field: 1, quotedReservedWordField: 1, date: 1, binary: 1, embeddedArrayDocument: 1, embeddedDocument: 1, embeddedArray: 1, embeddedDocumentArray: 1, nullValue: 1, coordinates.latitude: 1 } 5f75c6d38788f14f816f80f0 loadTest.saildrone { RH_MEAN: 1 } When running this command, you'll get all recommended indexes for the given process, along with the related namespace. You can use this information to make a decision on creating new indexes that could improve different operations over your database. To automate this process, we can write a small script to create all recommended indexes. This script will leverage the existing mongocli command to create rolling indexes. Then we’ll rerun our index recommendations and transform that into arguments to the create index command.: mongocli atlas performanceAdvisor suggestedIndexes ls \ --processName atlas-00-01.gcp.mongodb.net:27017 \ | awk 'BEGIN{ORS=" ";}{if (NR!=1){split($2,a,"."); print a[1]; print a[2]; for(i=4;i<=NF-1;++i)printf $i} printf"\n"}' \ | xargs -n3 sh -c 'echo "Creating index db: $1 collection: $2 index: $3"; mongocli atlas clusters indexes create --clusterName slowQueriesDemo --db $1 --collection $2 --key $3' sh This more advanced example shows how to combine or pipe different commands and automate different workflows, in this case saying goodbye to any slow queries. This is just one of the many things you can do with mongocli. We are constantly adding new features, so let us know what you would like to see next . Ready to see for yourself? Try MongoDB Atlas today! Sign up now Get MongoDB CLI! Download

October 19, 2020