How to find all the collections from mongoose

mongodb, mongoose

Solution

You can use the collectionNames function to return a list of collections.

db.on('open', function(){
  mongoose.connection.db.collectionNames(function(error, names) {
    if (error) {
      throw new Error(error);
    } else {
      names.map(function(cname) {
        console.log(cname.name);
      });
    }
  });
});

=> database1.system.indexes
=> database1.users
=> database1.posts

Problem

I am supposed to find all the collections stored in the a mongo database. ``` require('../app/models/schemas'); //loading application schemas mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); var collections = db.collections(); console.log(collections); ``` Here collections prints a combined `'json'` data of all the schemas. But i want to find all the collections stored in the mongo test database. How to achieve it with mongoose?

Original source

Related problems