I have found a few answers for listing collections in the shell but all the answers I have found for listing collections in a nodejs script seem to have been deprecated, answers like collectionNames
and moongose.connection.db
return has no method.
To list all collections in Mongo shell, you can use the function getCollectionNames().
In MongoDB, you can use the show dbs command to list all databases on a MongoDB server. This will show you the database name, as well as the size of the database in gigabytes. You can select any database using the use statement and work on it.
Use the listCollections Command to List All Collections in the MongoDB Shell. The administrator command listCollections returns the name and options of all collections and views in the database.
In the 2.0 version of the MongoDB driver for node.js you can use listCollections
to get a cursor that contains the information of all collections. You can then call toArray
on the cursor to retrieve the info.
db.listCollections().toArray(function(err, collInfos) { // collInfos is an array of collection info objects that look like: // { name: 'test', options: {} } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With