node.js 试图从猫鼬那里获取收藏列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19961387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:16:41  来源:igfitidea点击:

Trying to get a list of collections from mongoose

node.jsmongodbmongoose

提问by Austin Davis

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

我正在尝试使用猫鼬返回 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:[email protected]: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;
    });

the only problem is that names returns as undefined. So is it even possible to return a list of collections using just vanilla mongoose?

唯一的问题是名称返回未定义。那么是否有可能仅使用 vanilla mongoose 返回集合列表?

采纳答案by Charlie Key

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;
    });
})

回答by Squivo

Just came across this answer and though it may have worked at the time it appears collectionNameshas been removed from the available function names in favour of listCollections

刚刚遇到这个答案,虽然它可能在它出现的时候有效,但collectionNames已从可用的函数名称中删除,以支持listCollections

This other stack overflow post has a working example: https://stackoverflow.com/a/29461274/4127352

另一个堆栈溢出帖子有一个工作示例:https: //stackoverflow.com/a/29461274/4127352

Here is the link to the original docs: http://mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/

这是原始文档的链接:http: //mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/

回答by Roger

Here is how I managed to obtain all the names on the connected db.

这是我如何设法获得连接的数据库上的所有名称。

var mongoose = require('mongoose');
var collections = mongoose.connections[0].collections;
var names = [];

Object.keys(collections).forEach(function(k) {
    names.push(k);
});

console.log(names);

This solution works good on mongoose 4.4.19.

此解决方案适用于mongoose 4.4.19

回答by zurfyx

If you are only working with Mongoose Models, that is all you need:

如果您只使用 Mongoose 模型,这就是您所需要的:

const connection = mongoose.connection;
Object.keys(connection.models).forEach((collection) => {
  // You can get the string name.
  console.info(collection);
  // Or you can do something else with the model.
  connection.models[collection].remove({});
});