试图从猫鼬那里得到一系列的收藏。

时间:2021-07-12 02:34:01

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

我正在尝试使用mongoose返回一个dbs集合的列表。我按照这里列出的方向,但是http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a- 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;
    });

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

唯一的问题是名称返回未定义。那么,仅仅使用香草猫鼬就可以返回一个集合列表吗?

4 个解决方案

#1


5  

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.collectionNames(function (err, names) {
        console.log(names); // [{ name: 'dbname.myCollection' }]
        module.exports.Collection = names;
    });
})

#2


34  

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

刚刚遇到了这个答案,虽然它在当时可能是有效的,但是集合名已经从可用的函数名中删除,以支持listCollections

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

另一个堆栈溢出post有一个工作示例:https://*.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/

#3


2  

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

下面是我如何获取连接的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.19上运行良好。

#4


0  

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

如果你只使用蒙哥模型,这就是你所需要的:

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

#1


5  

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.collectionNames(function (err, names) {
        console.log(names); // [{ name: 'dbname.myCollection' }]
        module.exports.Collection = names;
    });
})

#2


34  

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

刚刚遇到了这个答案,虽然它在当时可能是有效的,但是集合名已经从可用的函数名中删除,以支持listCollections

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

另一个堆栈溢出post有一个工作示例:https://*.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/

#3


2  

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

下面是我如何获取连接的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.19上运行良好。

#4


0  

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

如果你只使用蒙哥模型,这就是你所需要的:

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