如何列出mongo shell中的所有集合?
在MongoDB shell中,如何列出我正在使用的当前数据库的所有集合?
你可以做…
JS(shell):
db.getCollectionNames() Node.js的:
 db.listCollections() 
非JS(仅限shell):
 show collections 
我称之为非JS的原因是因为:
 $ mongo prodmongo/app --eval "show collections" MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app --eval "db.getCollectionNames()" MongoDB shell version: 3.2.10 connecting to: prodmongo/app [ "Profiles", "Unit_Info" ] 
 如果你真的想要这个甜蜜,甜蜜的show collections输出,你可以: 
 $ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')" MongoDB shell version: 3.2.10 connecting to: prodmongo/app Profiles Unit_Info 
 > show collections 
 将列出当前所选数据库中的所有集合,如命令行帮助( help )中所述。 
如何列出我正在使用的当前数据库的所有集合?
3方法
-  show collections
-  show tables
-  db.getCollectionNames()
列出所有数据库 :
 show dbs 
input或使用给定的数据库:
 use databasename 
列出所有集合 :
 show collections 
输出:
collection1 collection2 system.indexes
(要么)
 show tables 
输出:
collection1 collection2 system.indexes
(要么)
 db.getCollectionNames() 
输出:
[ "collection1", "collection2", "system.indexes" ]
input或使用给定的集合
 use collectionname 
 > show tables 
它给出了卡梅隆的答案相同的结果。
除了其他人build议的选项:
 show collections //output every collection show tables db.getCollectionNames() //shows all collections as a list 
如果你想知道每个集合是如何创build的(例如它是一个特定大小的封闭集合),还有另一种方法可以非常方便,
 db.system.namespaces.find() 
首先你需要使用一个数据库来显示其中的所有集合/表格。
 >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 
 您可以使用show tables或show collections 
尝试:
 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 
用于显示mongoDb数据库中所有集合的命令是
 show collections 
在运行show collections命令之前,您必须select数据库
 use mydb //mydb is the name of the database being selected 
要查看所有数据库,可以使用该命令
 show dbs // shows all the database names present 
欲了解更多信息,请访问此链接: http : //docs.mongodb.org/manual/tutorial/getting-started/
如果你想显示来自mongodb shell(命令行)的所有集合,请使用shell帮助程序
 show collections 
显示当前数据库的所有集合。 如果你想从你的应用程序中获得所有的collections列表,那么你可以使用mongodb数据库方法
 db.getCollectionNames() 
有关更多信息,请参阅http://docs.mongodb.org/manual/reference/mongo-shell/
mongoshell上的以下命令是常见的
 show databases show collections 
也,
 show dbs use mydb db.getCollectionNames() 
有时查看所有集合以及作为整个名称空间一部分的集合上的索引是非常有用的:
以下是你将如何做到这一点:
  db.getCollectionNames().forEach(function(collection) { indexes = db[collection].getIndexes(); print("Indexes for " + collection + ":"); printjson(indexes); }); 
在这三个命令和这个片段之间,你应该被覆盖!
On> = 2.x,你可以做
 db.listCollections() 
在1.x上,你可以做
 db.getCollectionNames() 
 我认为最大的混乱之一是你可以用mongo (或交互式/混合shell)与mongo --eval (或纯粹的JavaScriptshell)做什么之间的区别。 我将这些有用的文件放在手边: 
- 交互式和脚本语言的区别
- Mongo Shell命令助手
 这里是一个脚本的例子,你可以用show命令做什么: 
 # List all databases and the collections in them mongo --eval " db.getMongo().getDBNames().forEach( function(v, i){ print( v + '\n\t' + db.getSiblingDB(v).getCollectionNames().join('\n\t') ) } ) " 
注意:这是一个很好的作为一个oneliner。 (但是在StackOverflow看起来很糟糕。)
 mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})" 
切换到数据库。 通过: – 使用{your_database_name}的例子:
 use friends 
哪里的朋友是你的数据库的名字。
然后写:-
 db.getCollectionNames() show collections 
这会给你collections的名字。
 > show dbs anuradhfirst 0.000GB local 0.000GB > use anuradhfirst switched to db anuradhfirst > show collections record 
-  使用mongo连接mongo数据库,这将启动连接。
-  然后运行show dbs命令,这会显示所有正在退出/可用的数据库。
-  然后select你想要的database在上面是anuradhfirst,然后运行use anuradhfirst将切换到你想要的数据库。
-  然后运行show collections命令,这将显示所选数据库中的所有collections。
显示集合
一旦切换到数据库,该命令通常在mongo shell上运行。
使用 – 显示集合这适用于mongo shell。
对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从3.0之前版本的mongo shell或3.0兼容版本之前的驱动版本运行
db.getCollectionNames(),则db.getCollectionNames()将不返回任何数据,甚至如果有现有collections。
有关更多详细信息,请参阅此处
使用数据库名称 – 将加载数据库。 显示集合 – 在加载的数据库中显示集合的列表。