在MongoDB shell中,如何列出当前使用的数据库的所有集合?


当前回答

对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从mongo shell的一个版本运行db.getCollectionNames()3.0之前的版本或3.0兼容版本之前的驱动程序版本,db.getCollectionNames()将不返回任何数据,即使存在现有集合。

有关详细信息,请参阅。

其他回答

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

除了其他人建议的选项外:

show collections  // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list

如果您想知道每个集合是如何创建的(例如,它是一个具有特定大小的上限集合),还有另一种方法非常方便:

db.system.namespaces.find()

列出mongo shell中的所有集合:

db.getCollectionNames()显示表显示表格注意:集合将显示您所在的当前数据库目前

在>=2.x时,您可以

db.listCollections()

在1.x上,您可以做到

db.getCollectionNames()

可以使用显示表或显示集合。