Reputation: 1990
I have multiple MongoDB collections, for example :
1. First
2. Second
3. Third
I just want to count number of all records from collections :
For that, I am using
db.First.find().count() // Show total number of records from First
db.Second.find().count() // Show total number of records from Second
db.Third.find().count() // Show total number of records from Third
And add all results to get total number of records.
How can i get total number of records from all collections by using single query ?
OR
What is the best way ?
Upvotes: 3
Views: 4036
Reputation: 71
you can write your own function in @mongodb to count all documents
var collections = db.getCollectionNames();
var count = 0;
for (var i = 0; i < collections.length; i++) {
count += db.getCollection(collections[i]).count();
}
Upvotes: 3
Reputation: 71
It will provide all documents count in db including system.js documents
db.stats().objects;
Upvotes: 4