【发布时间】:2012-02-10 13:40:17
【问题描述】:
在 MongoDB shell 中,如何列出我正在使用的当前数据库的所有集合?
【问题讨论】:
标签: mongodb mongo-shell
在 MongoDB shell 中,如何列出我正在使用的当前数据库的所有集合?
【问题讨论】:
标签: mongodb mongo-shell
你可以...
JavaScript(外壳):
db.getCollectionNames()
Node.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
【讨论】:
db.listCollections() 作为此处显示的答案并以绿色选中?否则,人们在得到这个答案时犯了我无数次犯过的同样的错误——并尝试使用db.getCollectionNames,错误又回来了db.collectionNames is not a function。
db.getCollectionNames() 仍然是 shell 的正确答案。
> show collections
将列出当前选定数据库中的所有集合,如命令行帮助 (help) 中所述。
【讨论】:
content 1145.586MB / 1506.855MB 为例。
如何列出我正在使用的当前数据库的所有集合?
show collectionsshow tablesdb.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
它给出的结果与 Cameron 的答案相同。
【讨论】:
除了其他人建议的选项:
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。
【讨论】:
如果要显示 MongoDB shell(命令行)中的所有集合,请使用 shell 帮助程序,
show collections
显示当前数据库的所有集合。 如果您想从应用程序中获取所有集合列表,则可以使用 MongoDB 数据库方法
db.getCollectionNames()
有关 MongoDB shell 帮助程序的更多信息,您可以查看 mongo Shell Quick Reference。
【讨论】:
以下命令在 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);
});
在这三个命令和这个sn-p之间,你应该被很好的覆盖了!
【讨论】:
我认为最大的困惑之一是使用 mongo(或交互式/混合 shell)与 mongo --eval(或纯 JavaScript 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')
)
}
)
"
注意:这非常适合作为单线。 (但在 StackOverflow 上看起来很糟糕。)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
【讨论】:
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
mongo 连接MongoDB 数据库。这将启动连接。show dbs 命令。这将显示所有现有/可用的数据库。database。在上面它是anuradhfirst。然后运行use anuradhfirst。这将切换到您想要的数据库。show collections 命令。这将显示您选择的数据库中的所有collections。【讨论】:
用于切换到数据库。
作者:
使用 {your_database_name} 示例:
use friends
其中friends 是您的数据库的名称。
然后写:
db.getCollectionNames()
show collections
这将为您提供集合的名称。
【讨论】:
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 } )
根据搜索字符串列出集合列表。
db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })
示例:查找名称中包含“import”的所有集合
db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
【讨论】:
在 >=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 部署,如果 你从一个版本的 mongo shell 运行
db.getCollectionNames()3.0之前的版本或者3.0之前的驱动兼容版本,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
【讨论】:
show collections
或
show tables
或
db.getCollectionNames();
【讨论】:
在mongo shell 中使用以下命令:
show collections
【讨论】: