在 nodejs 脚本中列出 mongo 数据库中的所有集合 [英] Listing all collections in a mongo database within a nodejs script

查看:38
本文介绍了在 nodejs 脚本中列出 mongo 数据库中的所有集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些在 shell 中列出集合的答案,但是我找到的在 nodejs 脚本中列出集合的所有答案似乎都已被弃用,像 collectionNamesmoongose 这样的答案.connection.db return 没有方法.

I have found a few answers for listing collections in the shell but all the answers I have found for listing collections in a nodejs script seem to have been deprecated, answers like collectionNames and moongose.connection.db return has no method.

推荐答案

在 node.js 的 MongoDB 驱动程序 2.0 版本中,您可以使用 listCollections 获取包含所有集合信息的游标.然后你可以调用 toArray 在光标上检索信息.

In the 2.0 version of the MongoDB driver for node.js you can use listCollections to get a cursor that contains the information of all collections. You can then call toArray on the cursor to retrieve the info.

db.listCollections().toArray(function(err, collInfos) {
    // collInfos is an array of collection info objects that look like:
    // { name: 'test', options: {} }
});

这篇关于在 nodejs 脚本中列出 mongo 数据库中的所有集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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