cof*_*der 728 mongodb nosql mongo-shell
在MongoDB shell中,如何列出我正在使用的当前数据库的所有集合?
Ada*_*Dev 1114
你可以做...
JS(shell):
db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)
Node.js的:
db.listCollections()
Run Code Online (Sandbox Code Playgroud)
非JS(仅限shell):
show collections
Run Code Online (Sandbox Code Playgroud)
我称之为非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"
]
Run Code Online (Sandbox Code Playgroud)
如果你真的想要甜蜜,甜蜜的show collections
输出,你可以:
$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info
Run Code Online (Sandbox Code Playgroud)
Cam*_*ron 412
> show collections
Run Code Online (Sandbox Code Playgroud)
将列出当前所选数据库中的所有集合,如命令行help(help
)中所述.
小智 255
如何列出我正在使用的当前数据库的所有集合?
show collections
show tables
db.getCollectionNames()
show dbs
Run Code Online (Sandbox Code Playgroud)
use databasename
Run Code Online (Sandbox Code Playgroud)
show collections
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)collection1 collection2 system.indexes
(要么)
show tables
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)collection1 collection2 system.indexes
(要么)
db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)[ "collection1", "collection2", "system.indexes" ]
use collectionname
Run Code Online (Sandbox Code Playgroud)
Sal*_*ali 29
除了其他人建议的选项:
show collections //output every collection
show tables
db.getCollectionNames() //shows all collections as a list
Run Code Online (Sandbox Code Playgroud)
如果您想知道每个集合是如何创建的(例如,它是具有特定大小的上限集合),还有另一种方法可以非常方便.
db.system.namespaces.find()
Run Code Online (Sandbox Code Playgroud)
Tar*_*pta 20
首先,您需要使用数据库来显示其中的所有集合/表.
>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
Run Code Online (Sandbox Code Playgroud)
Ami*_*esh 16
1. show collections; // Display all collections
2. show tables // Display all collections
3. db.getCollectionNames(); // Return array of collection. Example :[ "orders", "system.profile" ]
Run Code Online (Sandbox Code Playgroud)
每个集合的详细信息:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
Run Code Online (Sandbox Code Playgroud)
根据搜索字符串列出集合列表。
db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })
Run Code Online (Sandbox Code Playgroud)
示例: 查找名称中包含“import”的所有集合
db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
Run Code Online (Sandbox Code Playgroud)
小智 14
你可以使用show tables
或show collections
Ind*_*ngh 13
尝试:
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
Run Code Online (Sandbox Code Playgroud)
kkk*_*kkk 11
用于显示mongoDb数据库中所有集合的命令是
show collections
Run Code Online (Sandbox Code Playgroud)
在运行show collections命令之前,您必须选择数据库
use mydb //mydb is the name of the database being selected
Run Code Online (Sandbox Code Playgroud)
要查看所有数据库,可以使用该命令
show dbs // shows all the database names present
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请访问此链接:http://docs.mongodb.org/manual/tutorial/getting-started/
Eng*_*mon 10
如果要显示mongodb shell(命令行)中的所有集合,请使用shell helper
show collections
Run Code Online (Sandbox Code Playgroud)
显示当前数据库的所有集合.如果要从应用程序中获取所有集合列表,则可以使用mongodb数据库方法
db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)
有关mongodb shell帮助器的更多信息,请访问 http://docs.mongodb.org/manual/reference/mongo-shell/
小智 10
mongoshell上的以下命令很常见
show databases
show collections
Run Code Online (Sandbox Code Playgroud)
也,
show dbs
use mydb
db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)
有时,查看集合中的所有集合以及索引是有用的,这些集合是整个命名空间的一部分:
这是你如何做到这一点:
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
Run Code Online (Sandbox Code Playgroud)
在3个命令和这个片段之间你应该被很好地覆盖!
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
Run Code Online (Sandbox Code Playgroud)
mongo
. 这将开始连接。show dbs
命令。这将向您显示所有现有/可用的数据库。database
你想要的。在上面它是anuradhfirst
。然后运行use anuradhfirst
。这将切换到您想要的数据库。show collections
命令。这将显示collections
您选择的数据库内部的所有内容。我认为最大的困惑之一是您可以使用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')
)
}
)
"
Run Code Online (Sandbox Code Playgroud)
注意:作为一个单件套确实很好。(但是在StackOverflow上看起来很糟糕。)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
Run Code Online (Sandbox Code Playgroud)
用于切换到数据库。
经过:
使用 {your_database_name}示例:
use friends
Run Code Online (Sandbox Code Playgroud)
其中friends
是您的数据库的名称。
然后写:
db.getCollectionNames()
show collections
Run Code Online (Sandbox Code Playgroud)
这将为您提供集合的名称。
归档时间: |
|
查看次数: |
635639 次 |
最近记录: |