Search code examples
node.jsmongodbmongoskin

How to get a list of all collections with mongoskin


The goal is to get a list of all the collections in the database using mongoskin.

I know you can enter the db.getCollectionNames() method in mongo shell to do this, but I haven't found a way to achieve the same in my app.

I have already looked at a similar post on SO (Is there a way to list collections with mongoskin?) and tried the posted solution without success.


Solution

  • It's collectionNames(). It returns databasename.collectionname (ex. "test.user") though... but you can do some string manipulations to get rid of that if you need.

    db.collectionNames(function(err, items) {
      items.forEach(function(item) {
        console.log(item.name);
      });
    });