简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM