Search code examples
node.jsmongodbexpressmonk

List Collection Names in MongoDB using Monk


In my Node.js (Express) app that uses Monk, I need to render a list of all collections in the MongoDB database.

Is there a way to get the list of collections in the database using Monk?


Solution

  • This wil basicaly do that, but it takes some digging into the underlying driver to do so:

    var db = require('monk')('localhost/test');
    
    db.on("open",function() {
      console.log(
        db.driver._native.command(
          { "listCollections": 1 },
          function(err,result) {
            console.log( result.cursor.firstBatch.map(function(el) {
              return el.name;
            }))
        }
      )
    );
    

    });

    The driver command is of course "listCollections" and those are the basic hoops you need to jump through to get there