试图从猫鼬那里获取收藏列表 [英] Trying to get a list of collections from mongoose
本文介绍了试图从猫鼬那里获取收藏列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 mongoose 返回一个 dbs 集合的列表.我正在遵循此处列出的说明,但 http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a-list-of-all-collections.所以这是我的代码
I'm trying to return a list of a dbs collections using mongoose. I'm following the directions set out here but http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a-list-of-all-collections. So here is my code
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;
});
唯一的问题是名称返回未定义.那么是否可以仅使用 vanilla mongoose 返回集合列表?
the only problem is that names returns as undefined. So is it even possible to return a list of collections using just vanilla mongoose?
推荐答案
尝试在连接后运行您的集合名称函数.
Try running your collection names function after connection.
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;
});
})
这篇关于试图从猫鼬那里获取收藏列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文