我想用mongoose返回一个dbs系列的列表.我遵循这里的指示,但是http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a-list-of-all-collections.这是我的代码

var mongoose = require('mongoose');
    //if (mongoose.connection.readyState == 0){//checks if already connected to the database
    console.log("creating connection to the database");
    var Config = require('../configs/config');
    var config = new Config();
    config = config.getConfig().db.dev;

    if (mongoose.connection.readyState = 0 ) {
    mongoose.connect("mongodb://austin:password1@paulo.mongohq.com:10023/test1");
    console.log('mongoose readyState is ' + mongoose.connection.readyState);
    }
    var collection;

    mongoose.connection.on('open', function (ref) {
        console.log('Connected to mongo server.');
    });

    //trying to get collection names
    mongoose.connection.db.collectionNames(function (err, names) {
        console.log(names); // [{ name: 'dbname.myCollection' }]
        module.exports.Collection = names;
    });

唯一的问题是名称返回为未定义.那么,仅仅使用香草mongoose 就可以返回Collection 列表吗?

推荐答案

连接后try 运行集合名称功能.

mongoose.connection.on('open', function (ref) {
    console.log('Connected to mongo server.');
    //trying to get collection names
    mongoose.connection.db.listCollections().toArray(function (err, names) {
        console.log(names); // [{ name: 'dbname.myCollection' }]
        module.exports.Collection = names;
    });
})

Mongodb相关问答推荐

从MongoDB中的嵌套数组中提取找到的值及其索引

根据参考图计算子文档列表的最大值

Mongo聚合的具体格式

Mongoose 聚合和多级组

如何在查找 foreignField 中使用通配符?

管道聚合mongodb同一$project阶段的计算字段?

MongoDB查询仅返回嵌入文档

Meteor 中的平均聚合查询

Mongo:统计一组文档中单词出现的次数

在mongodb中实现分页

Ruby 按键值分组哈希

Cannot connect to MongoDB errno:61

Spring Data MongoDB 中的独特之处

如何使用 MongoDB C# 驱动程序有条件地组合过滤器?

120 个 mongodb 集合与单个集合 - 哪个更有效?

MongoDb 连接被拒绝

是否可以使用聚合框架对 MongoDB 中的 2 个字段求和?

MongoDb - 利用多 CPU 服务器进行写入繁重的应用程序

如何使用 mongodb-java-driver 进行 upsert

哪个数据库适合我的应用程序 mysql 或 mongodb ?使用 Node.js 、 Backbone 、 Now.js