I am new and I have a task.
Need to run Few queries on multiple replica sets to get information like
- how many nodes each replica has
- database count
- collection Count so on.
Kindly share me query to run script in multiple replica sets.
Thank you.
I am new and I have a task.
Need to run Few queries on multiple replica sets to get information like
Hi @Krishna_Sai1,
You can get the information about
Kindly run the below script in your MongoShell:
// Initialize an empty array to store the database information
var dbInfoArray = [];
// Fetch the list of databases
var databases = db.adminCommand({ listDatabases: 1, nameOnly: true }).databases;
// Add total databases count and total nodes to the array
dbInfoArray.push({
"Total Databases Count": databases.length,
"Total Nodes": rs.status() ? rs.status().members.length : "N/A" // Check if rs.status() is available
});
// Iterate over each database
databases.forEach(function(data) {
// Get the count of collections in the current database
var collCount = db.getSiblingDB(data.name).getCollectionNames().length;
// Add database information to the array
dbInfoArray.push({
"dbName": data.name,
"collectionCount": collCount
});
});
// Print the entire array
printjson(dbInfoArray);
You can run this in each replica set will give you the required information.
If you required to get information with specifications follow the below:
//how many nodes each replica has
rs.status().members.length
//database count
db.adminCommand({ listDatabases: 1, nameOnly: true }).databases.length
//collection Count so on a specific database
db.getSiblingDB("database name").getCollectionNames().length
Hope it will help you!
Thank you.