如何列出mongo shell中的所有集合?

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)

  • @niftylettuce这个问题是关于MongoDB shell,而不是node.js驱动程序.[`db.getCollectionNames()`](https://docs.mongodb.org/manual/reference/method/db.getCollectionNames/#db.getCollectionNames)仍然是shell的正确答案. (22认同)
  • [`db.getCollectionNames()`被删除,转而使用`db.listCollections()`](http://mongodb.github.io/node-mongodb-native/2.0/meta/changes-from-1.0/) (18认同)
  • 我们可以得到`db.listCollections()`作为这里显示的答案并以绿色检查?否则人们犯了同样的错误,当他们来到这个答案时我做了无数次 - 并尝试使用`db.getCollectionNames`并且错误返回`db.collectionNames不是函数`. (6认同)

Cam*_*ron 412

> show collections
Run Code Online (Sandbox Code Playgroud)

将列出当前所选数据库中的所有集合,如命令行help(help)中所述.

  • 您不能在脚本中使用show collections输出,但可以使用x = db.getCollectionNames()来获取所有名称的数组. (2认同)

小智 255

如何列出我正在使用的当前数据库的所有集合?

3方法

  • 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)

输出:

collection1  
collection2  
system.indexes
Run Code Online (Sandbox Code Playgroud)

(要么)

show tables
Run Code Online (Sandbox Code Playgroud)

输出:

collection1  
collection2  
system.indexes
Run Code Online (Sandbox Code Playgroud)

(要么)

db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)

输出:

[ "collection1", "collection2", "system.indexes" ]
Run Code Online (Sandbox Code Playgroud)

输入或使用给定的集合

use collectionname
Run Code Online (Sandbox Code Playgroud)

  • nope,`use`是使用数据库,与集合无关 (7认同)
  • 我们还可以使用 db.collections (2认同)

Kev*_*ith 51

> show tables

它给出了与Cameron的答案相同的结果.


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)
  • 对于具有所需访问权限(授予对数据库的 listCollections 操作的权限)的用户,该方法会列出数据库的所有集合的名称。
  • 对于没有所需访问权限的用户,该方法仅列出用户具有权限的集合。例如,如果用户在数据库中查找了特定集合,则该方法将仅返回该集合。

根据搜索字符串列出集合列表。

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 tablesshow collections

  • @LalitKumarB:怎么会这样?根据其他答案,这是实际可能有效的合适答案。至少它是一种回答的尝试。它是什么,是对已经发布了多个正确答案的非常古老的问题的答案。 (3认同)

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个命令和这个片段之间你应该被很好地覆盖!


Cha*_*era 6

> 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您选择的数据库内部的所有内容。


Bru*_*sky 6

我认为最大的困惑之一是您可以使用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)


Sha*_*dit 6

用于切换到数据库。

经过:

使用 {your_database_name}示例:

use friends
Run Code Online (Sandbox Code Playgroud)

其中friends是您的数据库的名称。

然后写:

db.getCollectionNames()
show collections
Run Code Online (Sandbox Code Playgroud)

这将为您提供集合的名称。