How do you list all collections inside a database in mongodb and count the number of rows for each of them?
I have migrated my MongoDB db to SQL, but I need to compare the number of rows on each of the collection and associated table to see if there's a match and if there's no match find out why.
Is there an easy way to do this? I know there's a question for listing all collections, but I also need the number of rows.
CodePudding user response:
Well, using the Mongo Shell
, you can try running these commands:
use databaseName
let collections = db.getCollectionNames();
collections.forEach(collection => { const docsCount = db.runCommand({count: collection}); print(``${collection}
${docsCount.n}``) })
This will print the number of documents in each collection in the database, on the Mongo Shell.