在MongoDB shell中,如何列出当前使用的数据库的所有集合?
当前回答
Try:
help // To show all help methods
show dbs // To show all dbs
use dbname // To select your db
show collections // To show all collections in selected db
其他回答
可以使用显示表或显示集合。
为此,我使用listCollections(支持MongoDB 3.0及更高版本)。
例子:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });
要获取更多信息,如集合的索引:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });
要仅打印集合名称:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})
我觉得这提供了更多的灵活性。
阅读更多:listCollections
如果有人使用Python和PyMongo:
db.list_collection_names()
> show collections
将列出当前选定DB中的所有集合,如命令行帮助(help)中所述。
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
使用mongo连接MongoDB数据库。这将启动连接。然后运行showdbs命令。这将显示所有退出/可用的数据库。然后选择所需的数据库。在上文中,它是第一个。然后运行use anuradhfirst。这将切换到所需的数据库。然后运行showcollections命令。这将显示所选数据库中的所有集合。
推荐文章
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?
- 如何监听MongoDB集合的变化?
- 如何在猫鼬排序?
- js的Mongoose.js字符串到ObjectId函数
- mongodb中使用ISODate的日期查询似乎无法正常工作
- 如何更新文档数组中的对象(嵌套更新)
- 在猫鼬模式中添加created_at和updated_at字段
- 如何更新mongodb中的多个数组元素
- MongoDB和Mongoose的区别
- MongoDB在v4之前不兼容ACID意味着什么?
- 显示所有集合中的所有内容
- MongoDB:更新一个字段上的每个文档
- 我如何在MongoDB中部分更新一个对象,以便新对象将覆盖/合并现有的一个