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


当前回答

如果有人使用Python和PyMongo:

db.list_collection_names()

其他回答

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

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

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

db.system.namespaces.find()

如果要显示MongoDB shell(命令行)中的所有集合,请使用shell助手,

show collections

显示当前数据库的所有集合。如果您想从应用程序中获取所有集合列表,那么可以使用MongoDB数据库方法

db.getCollectionNames()

有关MongoDB shell助手的更多信息,请参阅mongoShell快速参考。

> show collections

将列出当前选定DB中的所有集合,如命令行帮助(help)中所述。

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 dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db