List number of documents in each collection

Hi, I’m tryng to list the count of documents for all colletions but my code doesn’t work:

use sample_training
for (var i = 0; i < collections.length; i++) {
    print(collections[i], db.getCollection(collections[i]).countDocuments())
}

I get this error:

ReferenceError: collections is not defined

What am I doing wrong?

this seems to work:

for (var i = 0; i < db.getCollectionInfos().length; i++) {
    print(
        db.getCollectionInfos()[i]['name'],
        db.getCollection(db.getCollectionInfos()[i]['name']).countDocuments())
}
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.