在nodejs脚本中列出mongo数据库中的所有集合 [英] Listing all collections in a mongo database within a nodejs script
本文介绍了在nodejs脚本中列出mongo数据库中的所有集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在shell中找到了一些列出列表的答案,但是我发现所有在nodejs脚本中列出列表的答案似乎都已被弃用,像collectionNames
和moongose.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版本中,您可以使用 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屋!
查看全文