How to get all the collections from all the MongoDB databases?


To get all the collections from all the databases, let us first get all the databases using the following query

> switchDatabaseAdmin = db.getSiblingDB("admin");
admin
> allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;

This will produce the following output

[
   {
      "name" : "admin",
      "sizeOnDisk" : 495616,
      "empty" : false
   },
   {
      "name" : "config",
      "sizeOnDisk" : 98304,
      "empty" : false
   },
   {
      "name" : "local",
      "sizeOnDisk" : 73728,
      "empty" : false
   },
   {
      "name" : "sample",
      "sizeOnDisk" : 1335296,
      "empty" : false
   },
   {
      "name" : "sampleDemo",
      "sizeOnDisk" : 278528,
      "empty" : false
   },
   {
      "name" : "studentSearch",
      "sizeOnDisk" : 262144,
      "empty" : false
   },
   {
      "name" : "test",
      "sizeOnDisk" : 8724480,
      "empty" : false
   }
]

Following is the query to get all the collection names from databases

> allDatabaseName.forEach(function(databaseName)
... {
...    db = db.getSiblingDB(databaseName.name);
...    collectionName = db.getCollectionNames();
...    collectionName.forEach(function(collectionName)
...    {
...       print(collectionName);
...    });
... });

This will produce the following output

clearingItemsInNestedArrayDemo
customIdDemo
deleteRecordDemo
documentExistsOrNotDemo
findAllExceptFromOneOrtwoDemo
mongoExportDemo
startup_log
arraySizeErrorDemo
basicInformationDemo
copyThisCollectionToSampleDatabaseDemo
deleteAllRecordsDemo
deleteDocuments
deleteDocumentsDemo
deleteSomeInformation
documentWithAParticularFieldValueDemo
employee
findListOfIdsDemo
findSubstring
getAllRecordsFromSourceCollectionDemo
getElementWithMaxIdDemo
internalArraySizeDemo
largestDocumentDemo
makingStudentInformationClone
oppositeAddToSetDemo
prettyDemo
returnOnlyUniqueValuesDemo
selectWhereInDemo
sourceCollection
studentInformation
sumOfValueDemo
truncateDemo
updateInformation
userInformation
copyThisCollectionToSampleDatabaseDemo
deleteDocuments
deleteDocumentsDemo
deleteInformation
employee
internalArraySizeDemo
prettyDemo
sourceCollection
updateInformation
userInformation
col1
col2
indexingForArrayElementDemo
removeObjectFromArrayDemo
specifyAKeyDemo
useVariableDemo
ConvertStringToDateDemo
Employee_Information
IdUpdateDemo
IndexingDemo
NotAndDemo
ProductsInformation
addCurrentDateTimeDemo
addFieldDemo
addNewFieldToEveryDocument
aggregateSumDemo
aggregationFrameworkWithOrMatchDemo
aggregationSortDemo
andOrDemo
arrayInnerElementsDemo
arrayLengthGreaterThanOne
arrayOfArraysDemo
avoidDuplicateEntriesDemo
caseInsensitiveDemo
caseInsesitiveDemo
castingDemo
changeDataType
changeType
charactersAllowedDemo
charactersDemo
checkFieldContainsStringDemo
checkFieldExistsOrNotDemo
checkSequenceDemo
collectionOnDifferentDocumentDemo
combinationOfArrayDemo
comparingTwoFieldsDemo
concatStringAndIntDemo
conditionalSumDemo
convertStringToNumberDemo
copyThisCollectionToSampleDatabaseDemo
countDemo
countPerformanceDemo
createSequenceDemo
creatingUniqueIndexDemo
dateDemo
deleteAllElementsInArrayDemo
deleteRecordDemo
demo.insertCollection
distinctAggregation
distinctCountValuesDemo
distinctRecordDemo
distinctWithMultipleKeysDemo
doubleNestedArrayDemo
embeddedCollectionDemo
employeeInformation
equivalentForSelectColumn1Column2Demo
fieldIsNullOrNotSetDemo
filterArray
findAllDuplicateKeyDocumentDemo
findAllNonDistinctDemo
findByMultipleArrayDemo
findDocumentDoNotHaveCertainFields
findDocumentNonExistenceFieldDemo
findDocumentWithObjectIdDemo
findDuplicateByKeyDemo
findDuplicateRecordsDemo
findMinValueDemo
findSpecificValue
findValueInArrayWithMultipleCriteriaDemo
firstDocumentDemo
firstItemInArrayToNewFieldDemo
getDistinctListOfSubDocumentFieldDemo
getFirstItemDemo
getIndexSizeDemo
getLastNRecordsDemo
getLastXRecordsDemo
getNThElementDemo
getParticularElementFromArrayDemo
getPartuclarElement
getSizeDemo
getSizeOfArray
gettingHighestValueDemo
groupByDateDemo
hideidDemo
identifyLastDocuementDemo
incrementValueDemo
incrementValueInNestedArrayDemo
indexDemo
indexOptimizationDemo
indexTimeDemo
index_Demo
indexingDemo
insertDemo
insertFieldWithCurrentDateDemo
insertIfNotExistsDemo
insertIntegerDemo
insertOneRecordDemo
listAllValuesOfCeratinFieldsDemo
matchBetweenFieldsDemo
mongoExportDemo
multipleOrDemo
my-collection
nestedArrayDemo
nestedIndexDemo
nestedObjectDemo
new_Collection
notLikeOpeartorDemo
numberofKeysInADocumentDemo
objectInAnArrayDemo
objectidToStringDemo
orConditionDemo
orDemo
orderDocsDemo
paginationDemo
performRegex
priceStoredAsStringDemo
priceStoredDemo
queryArrayElementsDemo
queryByKeyDemo
queryBySubFieldDemo
queryForBooleanFieldsDemo
queryInSameDocumentsDemo
queryToEmbeddedDocument
queryingMongoDbCaseInsensitiveDemo
regExpOnIntegerDemo
regexSearchDemo
removeArrayDemo
removeArrayElement
removeArrayElementByItsIndexDemo
removeArrayElements
removeDocumentOnBasisOfId
removeDuplicateDocumentDemo
removeDuplicateDocuments
removeElementFromDoublyNestedArrayDemo
removeFieldCompletlyDemo
removeMultipleDocumentsDemo
removeObject
removingidElementDemo
renameFieldDemo
retrieveValueFromAKeyDemo
retunFieldInFindDemo
returnQueryFromDate
reverseRegexDemo
s
searchArrayDemo
searchDocumentDemo
searchDocumentWithSpecialCharactersDemo
searchMultipleFieldsDemo
secondDocumentDemo
selectInWhereIdDemo
selectMongoDBDocumentsWithSomeCondition
selectRecordsHavingKeyDemo
selectSingleFieldDemo
singleFieldDemo
sortDemo
sortInnerArrayDemo
sortingDemo
sourceCollection
sqlLikeDemo
stringFieldLengthDemo
stringToObjectIdDemo
test.js
translateDefinitionDemo
unconditionalUpdatesDemo
uniqueIndexOnArrayDemo
unprettyJsonDemo
unwindOperatorDemo
updateDemo
updateExactField
updateIdDemo
updateManyDocumentsDemo
updateNestedValueDemo
updateObjects
updatingEmbeddedDocumentPropertyDemo
userStatus
raja
Published on 11-Apr-2019 09:07:34
Advertisements