How to list all collections from a particular MongoDB database?


If you want to list all collections from a particular database then you need to switch the database first. The query is as follows −

> use sample;
switched to db sample
> db.getCollectionNames();

The following is the output −

[
   "copyThisCollectionToSampleDatabaseDemo",
   "deleteDocuments",
   "deleteDocumentsDemo",
   "deleteInformation",
   "employee",
   "internalArraySizeDemo",
   "sourceCollection",
   "updateInformation",
   "userInformation"
]

An alternate query can be the following −

> show collections;

The following is the output −

copyThisCollectionToSampleDatabaseDemo
deleteDocuments
deleteDocumentsDemo
deleteInformation
employee
internalArraySizeDemo
sourceCollection
updateInformation
userInformation

Updated on: 30-Jul-2019

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements