MongoDB SHOW Collections

MongoDB SHOW Collections is used to find the list of collections in current database. We can get the list of collections from MongoDB database using below methods:

1. show collections
2. show tables
3. db.getCollectionNames()
4. db.runCommand()
5. mongo database_name –eval “db.getCollectionNames()”

MongoDB SHOW CollectionsMongoDB SHOW Collections Examples:

1. Get list of MongoDB collections using ‘show collections’

use r2schools show collections

MongoDB show collections

2. Get list of MongoDB collections using ‘show tables’

use r2schools show tables

3. Get list of collections in MongoDB using db.getCollectionNames()

use r2schools db.getCollectionNames()

4. Get list of collections in MongoDB using db.runCommand()

use r2schools db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})

MongoDB show collections

5. Get list of collections in MongoDB using mongo shell

mongo r2schools --eval "db.getCollectionNames()"

So in this article, we have seen different ways to get the list of collection from MongoDB database.