How to list all collections in the mongo shell?

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


You can do...

JS (shell):

db.getCollectionNames()

node.js:

db.listCollections()

non-JS (shell only):

show collections

The reason I call that non-JS is because:

$ 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"
]

If you really want that sweet, sweet show collections output, you can:

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

> show collections

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


how do I list all collections for the current database that I'm using?

3 Methods

  • show collections
  • show tables
  • db.getCollectionNames()

  • To list all databases:

    show dbs
    

    To enter or use given database:

    use databasename
    

    To list all collections:

    show collections
    

    Output:

    collection1  
    collection2  
    system.indexes
    

    (or)

    show tables
    

    Output:

    collection1  
    collection2  
    system.indexes
    

    (or)

    db.getCollectionNames()
    

    Output:

    [ "collection1", "collection2", "system.indexes" ]
    

    To enter or use given collection

    use collectionname
    
    链接地址: http://www.djcxy.com/p/50232.html

    上一篇: Mongodb更新深层嵌套的子文档

    下一篇: 如何列出mongo shell中的所有集合?