Reputation: 3242
In mongoDB finding the number of documents within a collection is easy using the count() function. Yet how can I find the number of documents within all user generated collections (excluding system collections such as db.system.indexes and db.system.users)?
A straight-forward approach would be to iterate over all collections and check that they are user generated, then sum their counts(). Yet I suspect that this will be inefficient when a large number of collections is present.
An alternative approach would be to use the db.stats() command. This gives me some basic information such as the number of indexes, extents, collections and objects. Yet objects is not the same as the number of user generated documents. It seems like the following formula can be used to compute the number of documents from the output of db.stats() (run this on the mongo shell):
docs = db.stats()['objects'] - db.stats()['collections'] -
db.stats()['numExtents'] - 2 * (db.stats()['indexes'] - 2) + 1
Is there any easier approach (which is also efficient for large databases) to find this?
Thanks
Upvotes: 1
Views: 680
Reputation: 222581
Your approach is correct. You either:
count()
thereAny approach would work nice. Information about the total number of elements in the collection is actually also available through db.collection.stats();
. So I assume that mongo will rather get it from there then to count it (if it does not do this, you can do this by yourself).
Another approach is to take data from stats
. Which I like much more, because you do not need to iterate anything. The only problem is that your formula is wrong, or at least it is wrong on my sort of data. I do not understand why do you subtract the number of collections and numExtents and also do this strange manipulations with indexes.
As you see from dbStats, objects are just the sum of all documents across all collections. As far as I remember apart of your collections there are only db.system.indexes
, db.system.users
and db.system.namespaces
. So the formula should be:
db.stats()['objects'] - db.system.indexes.count() - db.system.namespaces.count() - db.system.users.count()
So I assume that this is the fastest way to achieve what you want.
Upvotes: 1