How to list all collections in the mongo shell ?



  • 3 Answer(s)

    Just we can try to do it

    JS (shell):

    db.getCollectionNames()
    

    node.js:

    db.listCollections()
    

    non-JS (only for shell):

    show collections
    

    The following below code can be a reason for non-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"
    ]
    

    show collections result, can be:

    $ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
    MongoDB shell version: 3.2.10
    connecting to: prodmongo/app
    Profiles
    Unit_Info
    
    Answered on December 21, 2018.
    Add Comment
    > show collections
    
    

    Here we will show all the collections in the currently selected DB, as stated in the command line help (help)

    Answered on December 22, 2018.
    Add Comment

    how to list all the collection in the database ?

    There are three methods

    show collections
    show collections
    db.getCollectionNames()

    To show entire database:

    show dbs
    

    Use specific database:

    use databasename
    

    To show entire collections

    show collections
    

    Result:

    collection1
    collection2
    system.indexes
    

    or else

    show tables
    

    Result:

    collection1
    collection2
    system.indexes
    

    or else

    db.getCollectionNames()
    

    Result:

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

    Use specific collection

    use collectionname
    
    Answered on December 22, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.