Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all collections in a mongo database within a nodejs script

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.

like image 770
Jake Avatar asked May 26 '15 23:05

Jake


People also ask

How do I show all collections in MongoDB?

To list all collections in Mongo shell, you can use the function getCollectionNames().

How do I list databases in Mongo shell?

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.

What command will return the full list of collections in a MongoDB database?

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.


1 Answers

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: {} } }); 
like image 170
JohnnyHK Avatar answered Sep 21 '22 18:09

JohnnyHK