mongodb 想从 Mongoose 那里拿到收藏品清单

h7appiyu  于 2023-10-16  发布在  Go
关注(0)|答案(7)|浏览(107)

我正试图使用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:[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;
    });

唯一的问题是name返回为undefined。那么,是否有可能只使用vanilla mongoose返回集合列表呢?

ru9i0ody

ru9i0ody1#

我刚刚看到这个答案,虽然它可能在当时工作,但它似乎已经从可用的函数名称中删除了collectionNames,而支持listCollections
这另一篇栈溢出文章有一个工作示例:https://stackoverflow.com/a/29461274/4127352
以下是原始文档的链接:http://mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/

cnjp1d6j

cnjp1d6j2#

尝试在连接后运行您的集合名称功能。

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

vq8itlhq3#

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

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

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

console.log(names);

此解决方案在mongoose 4.4.19上运行良好。

hfyxw5xn

hfyxw5xn4#

我尝试了所有以前的答案,没有收到一个积极的结果。但是我已经用这个代码得到了集合数组:

let listOfCollections = Object.keys(mongoose.connection.collections);
3df52oht

3df52oht5#

如果您只与Mongoose Models合作,这就是您所需要的:

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

0mkxixxg6#

类似于上面的顶部答案,但此代码显示每个集合的名称,而没有其他添加的信息(如类型,选项,信息等)。

const namesList = [];
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) {
    for (let i = 0; i < names.length; i++) {
      // gets only the name and adds it to a list
      const nameOnly = names[i].name;
      namesList.push(nameOnly);
    }
    console.log(namesList);
    module.exports.Collection = namesList;
  });
});
drkbr07n

drkbr07n7#

import mongoose from "mongoose";

 // code to create a connection..etc.

 const db = mongoose.connection.db;
 const collections = await db.listCollections().toArray();
 console.log(collections);

这对我很有效。此外,您可以循环通过输出来获得所需的输出格式。

**ps:**根据MongoDb doc,我们有某些字段,如nameOnlyfilter,可应用于listCollections查询。但这对我和Mongoose不起作用。如果你有什么收获,请添加评论。

谢谢你,谢谢

相关问题