在MongoDB shell中,如何列出我正在使用的当前数据库的所有集合?
您可以做...
JavaScript(shell):
db.getCollectionNames()
Node.js:
db.listCollections()
非JavaScript(仅外壳):
show collections
我之所以使用非JavaScript的原因是:
$ 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
如果要显示mongodb shell(命令行)中的所有集合,请使用shell帮助器
show collections
显示当前数据库的所有集合。如果要从应用程序中获取所有收集列表,则可以使用mongodb数据库方法
db.getCollectionNames()
有关更多信息,您可以看到mongodb shell助手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);
});
在这三个命令和这段代码之间,您应该被很好地介绍!
[我认为最大的困惑之一是mongo
(或交互式/混合外壳)与mongo --eval
(或纯JavaScript外壳)之间的区别。我将这些有用的文档放在手边:
这里是编写脚本的示例,否则您可能会用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')
)
}
)
"
注意:作为一个单一的内衬确实很好。 (但是在StackOverflow上看起来很糟糕。)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
在> = 2.x,可以执行
db.listCollections()
在1.x上可以执行
db.getCollectionNames()
用于切换到数据库。通过:-使用{your_database_name}示例:
use friends
其中,朋友是您的数据库的名称。
然后写:-
db.getCollectionNames()
show collections
这将为您提供集合的名称。
列出mongo shell中的所有集合:
- db.getCollectionNames()
- 显示收藏集
- 显示表格
注意:集合将从您所在的当前数据库显示当前
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
mongo
与mongo数据库连接,这将启动连接。show dbs
命令,这将向您显示所有现有/可用的数据库。database
。在其上方的是anuradhfirst
,然后运行use anuradhfirst
。这将切换到所需的数据库。show collections
命令,这将显示所选数据库中的所有collections
。显示收藏集
一旦切换到数据库,此命令通常在mongo shell上有效。
对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果您从mongo shell版本运行
db.getCollectionNames()
3.0之前的版本或3.0兼容版本之前的驱动程序版本,db.getCollectionNames()
将不返回任何数据,即使有现有集合。
有关更多详细信息,请参阅this
1. show collections; //Display all collection
2. show tables //Display all collection
3. db.getCollectionNames(); // Retuen array of collection Example :[ "orders", "system.profile" ]
每个收藏的详细信息
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
> show collections
将列出当前所选数据库中的所有集合,如命令行帮助(help
)中所述。
为此,我使用listCollections
(支持mongo 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)})
我觉得这提供了更大的灵活性。
更多:https://docs.mongodb.com/manual/reference/command/listCollections/
show collections
使用mongo shell中的以下命令:-显示收藏集
如何列出我正在使用的当前数据库的所有集合?
show collections
show tables
db.getCollectionNames()
show dbs
use databasename
show collections
输出:
collection1 collection2 system.indexes
(或)
show tables
输出:
collection1 collection2 system.indexes
(或)
db.getCollectionNames()
输出:
[ "collection1", "collection2", "system.indexes" ]
use collectionname
> show tables
它的结果与卡梅伦的答案相同。
除了其他人建议的选项:
show collections // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list
如果您想知道每个集合是如何创建的(例如,它是一个具有特定大小的带帽集合),还有另一种非常方便的方法:
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命令之前,您必须选择数据库
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/