首页 文章

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

提问于
浏览
678

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

21 回答

  • 0

    使用来自mongo shell的以下命令: - show collections

  • 14

    为此,我使用 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/

  • 399

    首先,您需要使用数据库来显示其中的所有集合/表 .

    >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
    
  • 244
    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 } )
    
    • 对于具有所需访问权限的用户(授予对数据库执行listCollections操作的权限),该方法将列出数据库的所有集合的名称 .

    • 对于没有所需访问权限的用户,该方法仅列出用户具有权限的集合 . 例如,如果用户在数据库中找到特定集合,则该方法将仅返回该集合 .

  • 28

    > show tables

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

  • 1037

    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);
    });
    

    在3个命令和这个片段之间你应该被很好地覆盖!

  • 3

    你可以做...

    JS(shell):

    db.getCollectionNames()
    

    Node.js的:

    db.listCollections()
    

    非JS(仅限shell):

    show collections
    

    我称之为非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"
    ]
    

    如果你真的想要那种甜美,甜美的输出,你可以:

    $ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
    MongoDB shell version: 3.2.10
    connecting to: prodmongo/app
    Profiles
    Unit_Info
    
  • 0
    > show collections
    

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

  • 20

    在> = 2.x,你可以做

    db.listCollections()
    

    在1.x你可以做到

    db.getCollectionNames()
    
  • 10

    用于切换到数据库 . 通过: - use 示例:

    use friends
    

    其中friends是数据库的名称 .

    然后写:-

    db.getCollectionNames()
    show collections
    

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

  • 11

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

    3方法

    • show collections

    • show tables

    • db.getCollectionNames()


    列出所有数据库:

    show dbs
    

    输入或使用给定的数据库:

    use databasename
    

    列出所有馆藏:

    show collections
    

    输出:

    集合1
    collection2
    system.indexes

    (要么)

    show tables
    

    输出:

    集合1
    collection2
    system.indexes

    (要么)

    db.getCollectionNames()
    

    输出:

    [“collection1”,“collection2”,“system.indexes”]


    输入或使用给定的集合

    use collectionname
    
  • 51

    尝试:

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

    展示收藏品

    切换到数据库后,此命令通常适用于mongo shell .

  • 2

    如果要显示mongodb shell(命令行)中的所有集合,请使用shell helper

    show collections
    

    显示当前数据库的所有集合 . 如果要从应用程序中获取所有集合列表,则可以使用mongodb数据库方法

    db.getCollectionNames()
    

    有关mongodb shell帮助器的更多信息,您可以看到http://docs.mongodb.org/manual/reference/mongo-shell/

  • 13

    你可以使用 show tablesshow collections

  • 2

    用于显示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/

  • 0

    List all collections from the mongo shell :

    db.getCollectionNames()show collections show tables注意:集合将从当前数据库中显示您当前所在的位置

  • 0
    > 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 .

  • 9

    对于使用WiredTiger存储引擎的MongoDB 3.0部署,如果从3.0之前的mongo shell版本或3.0兼容版本之前的驱动程序版本运行db.getCollectionNames(),则db.getCollectionNames()将不返回任何数据,即使有现有的收藏品 .

    有关详细信息,请参阅this

  • 1

    我认为最大的混淆之一是你可以用 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')
                )
            }
        )
    "
    

    注意:这非常适合作为oneliner . (但在StackOverflow上看起来很糟糕 . )

    mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
    
  • 0

    除了其他人建议的选项:

    show collections  //output every collection
    show tables
    db.getCollectionNames() //shows all collections as a list
    

    还有另一种方法,如果你真的很方便想知道每个集合是如何创建的(例如,它是一个具有特定大小的上限集合)

    db.system.namespaces.find()
    

相关问题