在 MongoDB shell 中,如何列出我正在使用的当前数据库的所有集合?
你可以做...
JavaScript(外壳):
db.getCollectionNames()
节点.js:
db.listCollections()
非 JavaScript(仅限 shell):
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
> show collections
将列出当前选定数据库中的所有集合,如命令行帮助 (help
) 中所述。
content 1145.586MB / 1506.855MB
例如。
如何列出我正在使用的当前数据库的所有集合?
三种方法
显示收藏
显示表格
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
对于那些来自关系 dbms 背景的人来说非常有帮助。
use
是使用数据库,与集合无关
> 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
尝试:
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 tables
或 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
有关详细信息,请访问 Getting Started。
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);
});
在这三个命令和这段代码之间,你应该得到很好的覆盖!
如果要显示 MongoDB shell(命令行)中的所有集合,请使用 shell 帮助程序,
show collections
显示当前数据库的所有集合。如果要从应用程序中获取所有集合列表,则可以使用 MongoDB 数据库方法
db.getCollectionNames()
有关 MongoDB shell 帮助程序的更多信息,您可以查看 mongo
Shell Quick Reference。
我认为最大的困惑之一是您可以使用 mongo
(或交互式/混合 shell)与 mongo --eval
(或纯 JavaScript shell)之间的区别。我把这些有用的文件放在手边:
交互式和脚本化 mongo 之间的区别
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')
)
}
)
"
注意:这非常适合作为单线。 (但在 Stack Overflow 上看起来很糟糕。)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
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) })
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
使用 mongo 连接 MongoDB 数据库。这将启动连接。
然后运行 show dbs 命令。这将向您显示所有现有/可用的数据库。
然后选择你想要的数据库。在上面它是anuradhfirst。然后运行使用 anuradhfirst。这将切换到您想要的数据库。
然后运行 show collections 命令。这将显示您选择的数据库中的所有集合。
用于切换到数据库。
经过:
使用 {your_database_name} 示例:
use friends
其中 friends
是您的数据库的名称。
然后写:
db.getCollectionNames()
show collections
这将为您提供集合的名称。
在 >=2.x 上,您可以执行
db.listCollections()
在 1.x 上你可以做
db.getCollectionNames()
db.getCollectionNames()
时,我得到 [ "users" ]
,因为我有一个 users 集合。如果我尝试 db.listCollections()
那么它会导致 [thread1] TypeError: db.listCollections is not a function : @(shell):1:1
列出 mongo
shell 中的所有集合:
db.getCollectionNames() 显示集合 显示表格 注意:集合将显示您当前所在的当前数据库
显示收藏
切换到数据库后,此命令通常在 MongoDB shell 上运行。
如果有人使用 Python 和 PyMongo:
db.list_collection_names()
对于使用 WiredTiger 存储引擎的 MongoDB 3.0 部署,如果您从 3.0 之前的 mongo shell 版本或 3.0 兼容版本之前的驱动程序版本运行 db.getCollectionNames(),则 db.getCollectionNames() 将不返回任何数据,甚至如果有现有的集合。
有关详细信息,请参阅this。
为此,我使用 listCollections
(支持 MongoDB 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)})
我觉得这提供了更多的灵活性。
阅读更多:listCollections
显示收藏
或者
显示表格
或者
db.getCollectionNames();
在 mongo
shell 中使用以下命令:
show collections
db.getCollectionNames()
was removed in favor ofdb.listCollections()
db.listCollections()
作为此处显示的答案并以绿色选中吗?否则,人们在得到这个答案时犯了我无数次犯过的同样的错误 - 并尝试使用db.getCollectionNames
并且错误又回来了db.collectionNames is not a function
。db.getCollectionNames()
仍然是 shell 的正确答案。node
答案不合适!请查看谷歌结果排名,像我这样的人带着想要快速解决方案的节点环境来到这里。 Node 广泛用于 mongo。但是,这里的节点解决方案对我不起作用;(