如何在mongoose中获取集合列表?

Jac*_*son 1 mongoose mongodb

如何在mongoose中获取名称收集列表?
较旧的答案 -
mongoose.connection.db.collectionNames不是一个函数(

Lui*_*dez 8

确实,mongoose.connection.db.collectionNames有人赞成mongoose.connection.db.listCollections.

const mongoose = require('mongoose');

const connection = mongoose.connect('mongodb://localhost:27017');

connection.on('open', function () {
    connection.db.listCollections().toArray(function (err, names) {
      if (err) {
        console.log(err);
      } else {
        console.log(names);
      }

      mongoose.connection.close();
    });
});
Run Code Online (Sandbox Code Playgroud)