如何从猫鼬中找到所有收藏 [英] How to find all the collections from mongoose

查看:59
本文介绍了如何从猫鼬中找到所有收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该找到mongo数据库中存储的所有集合.

I am supposed to find all the collections stored in the a mongo database.

require('../app/models/schemas'); //loading application schemas
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var collections = db.collections();
console.log(collections);

此处的集合将打印所有架构的组合'json'数据.

Here collections prints a combined 'json' data of all the schemas.

但是我想找到所有存储在mongo测试数据库中的集合. 如何用猫鼬实现它?

But i want to find all the collections stored in the mongo test database. How to achieve it with mongoose?

推荐答案

您可以使用collectionNames函数返回集合列表.

You can use the collectionNames function to return a list of collections.

db.on('open', function(){
  mongoose.connection.db.collectionNames(function(error, names) {
    if (error) {
      throw new Error(error);
    } else {
      names.map(function(cname) {
        console.log(cname.name);
      });
    }
  });
});

=> database1.system.indexes
=> database1.users
=> database1.posts

这篇关于如何从猫鼬中找到所有收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆