Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total number of records across all databases in MongoDB?

Tags:

mongodb

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.

like image 512
Abhinandan Kothari Avatar asked Sep 30 '16 07:09

Abhinandan Kothari


People also ask

How do I find the number of records in MongoDB?

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.

How do I sum all files in MongoDB?

To get sum the value of a key across all documents in a MongoDB collection, you can use aggregate().

What is the command to show a list of all MongoDB databases?

Listing all the databases in mongoDB console is using the command show dbs .

How do I show all collection data in MongoDB?

To list all collections in Mongo shell, you can use the function getCollectionNames().


1 Answers

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);
like image 174
styvane Avatar answered Oct 13 '22 17:10

styvane