How to list all collections in the Mongo shell?


To list all collections in Mongo shell, you can use the function getCollectionNames().

The syntax is as follows −

db.getCollectionNames();

You can use another command which is collections. The syntax is as follows −

show collections;

To list all collections in Mongo, use the above two functions. The query is as follows −

> db.getCollectionNames();

The following is the output −

[
   "ConvertStringToDateDemo",
   "IdUpdateDemo",
   "ProductsInformation",
   "addFieldDemo",
   "addNewFieldToEveryDocument",
   "arrayInnerElementsDemo",
   "arrayLengthGreaterThanOne",
   "arrayOfArraysDemo",
   "caseInsensitiveDemo",
   "changeDataType",
   "changeType",
   "charactersAllowedDemo",
   "charactersDemo",
   "checkFieldContainsStringDemo",
   "checkSequenceDemo",
   "combinationOfArrayDemo",
   "conditionalSumDemo",
   "convertStringToNumberDemo",
   "copyThisCollectionToSampleDatabaseDemo",
   "countDemo",
   "createSequenceDemo",
   "distinctCountValuesDemo",
   "distinctRecordDemo",
   "distinctWithMultipleKeysDemo",
   "employeeInformation",
   "filterArray",
   "findAllDuplicateKeyDocumentDemo",
   "findByMultipleArrayDemo",
   "findDuplicateByKeyDemo",
   "findDuplicateRecordsDemo",
   "findSpecificValue",
   "findValueInArrayWithMultipleCriteriaDemo",
   "getLastNRecordsDemo",
   "getParticularElementFromArrayDemo",
   "getPartuclarElement",
   "getSizeOfArray",
   "groupByDateDemo",
   "incrementValueInNestedArrayDemo",
   "insertIfNotExistsDemo",
   "nestedArrayDemo",
   "notLikeOpeartorDemo",
   "regExpOnIntegerDemo",
   "removeArrayDemo",
   "removeArrayElement",
   "removeArrayElements",
   "removeDuplicateDocumentDemo",
   "removeElementFromDoublyNestedArrayDemo",
   "removeFieldCompletlyDemo",
   "removeObject",
   "renameFieldDemo",
   "reverseRegexDemo",
   "searchArrayDemo",
   "selectSingleFieldDemo",
   "singleFieldDemo",
   "sortDemo",
   "sortInnerArrayDemo",
   "sourceCollection",
   "stringFieldLengthDemo",
   "test.js",
   "uniqueIndexOnArrayDemo",
   "unwindOperatorDemo",
   "updateExactField",
   "updateIdDemo",
   "updateObjects"
]

Here is the second query to display all collections from Mongo. The query is as follows −

> show collections;

The following is the output −

ConvertStringToDateDemo
IdUpdateDemo
ProductsInformation
addFieldDemo
addNewFieldToEveryDocument
arrayInnerElementsDemo
arrayLengthGreaterThanOne
arrayOfArraysDemo
caseInsensitiveDemo
changeDataType
changeType
charactersAllowedDemo
charactersDemo
checkFieldContainsStringDemo
checkSequenceDemo
combinationOfArrayDemo
conditionalSumDemo
convertStringToNumberDemo
copyThisCollectionToSampleDatabaseDemo
countDemo
createSequenceDemo
distinctCountValuesDemo
distinctRecordDemo
distinctWithMultipleKeysDemo
employeeInformation
filterArray
findAllDuplicateKeyDocumentDemo
findByMultipleArrayDemo
findDuplicateByKeyDemo
findDuplicateRecordsDemo
findSpecificValue
findValueInArrayWithMultipleCriteriaDemo
getLastNRecordsDemo
getParticularElementFromArrayDemo
getPartuclarElement
getSizeOfArray
groupByDateDemo
incrementValueInNestedArrayDemo
insertIfNotExistsDemo
nestedArrayDemo
notLikeOpeartorDemo
regExpOnIntegerDemo
removeArrayDemo
removeArrayElement
removeArrayElements
removeDuplicateDocumentDemo
removeElementFromDoublyNestedArrayDemo
removeFieldCompletlyDemo
removeObject
renameFieldDemo
reverseRegexDemo
searchArrayDemo
selectSingleFieldDemo
singleFieldDemo
sortDemo
sortInnerArrayDemo
sourceCollection
stringFieldLengthDemo
test.js
uniqueIndexOnArrayDemo
unwindOperatorDemo
updateExactField
updateIdDemo
updateObjects

Updated on: 30-Jul-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements