Is there any way through which i can get total number of records across all the databases in MongoDB server.
There is command for finding total records in a particular collection of a databases. But i want to get the count of total number of records across all databases.
count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.
To get sum the value of a key across all documents in a MongoDB collection, you can use aggregate().
Listing all the databases in mongoDB console is using the command show dbs .
To list all collections in Mongo shell, you can use the function getCollectionNames().
Simply use db.stats
db.stats().objects
The objects
property return the count of documents in the database across all collections.
To get the total number across all databases, you may need to do something like this:
let client = db.getMongo();
client.getDBNames().filter( name => name !== 'local')
.map( elt => client.getDB(elt).stats().objects )
.reduce( ( acc, cur ) => acc + cur, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With