在MongoDB shell中,如何列出当前使用的数据库的所有集合?
当前回答
用于切换到数据库。
By:
使用{your_database_name}示例:
use friends
其中friends是数据库的名称。
然后写下:
db.getCollectionNames()
show collections
这将为您提供集合的名称。
其他回答
用于显示MongoDB数据库中所有集合的命令是
show collections
在运行show collections命令之前,必须选择数据库:
use mydb // mydb is the name of the database being selected
要查看所有数据库,可以使用以下命令
show dbs // Shows all the database names present
有关详细信息,请参阅入门。
如果要显示MongoDB shell(命令行)中的所有集合,请使用shell助手,
show collections
显示当前数据库的所有集合。如果您想从应用程序中获取所有集合列表,那么可以使用MongoDB数据库方法
db.getCollectionNames()
有关MongoDB shell助手的更多信息,请参阅mongoShell快速参考。
对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从mongo shell的一个版本运行db.getCollectionNames()3.0之前的版本或3.0兼容版本之前的驱动程序版本,db.getCollectionNames()将不返回任何数据,即使存在现有集合。
有关详细信息,请参阅。
如果有人使用Python和PyMongo:
db.list_collection_names()
1. show collections; // Display all collections
2. show tables // Display all collections
3. db.getCollectionNames(); // Return array of collection. Example :[ "orders", "system.profile" ]
每个集合的详细信息:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
对于具有所需访问权限(授予数据库listCollections操作的权限)的用户,该方法将列出数据库的所有集合的名称。对于没有所需访问权限的用户,该方法仅列出用户具有权限的集合。例如,如果用户在数据库中找到了特定集合,则该方法将仅返回该集合。
根据搜索字符串列出集合列表。
db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })
示例:查找名称中包含“import”的所有集合
db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
推荐文章
- 在MongoDB中查找重复的记录
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 在猫鼬,我如何排序的日期?(node . js)
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- Redis比mongoDB快多少?
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?
- 如何监听MongoDB集合的变化?
- 如何在猫鼬排序?
- js的Mongoose.js字符串到ObjectId函数
- mongodb中使用ISODate的日期查询似乎无法正常工作
- 如何更新文档数组中的对象(嵌套更新)
- 在猫鼬模式中添加created_at和updated_at字段