List Collection Names of MongoDB Database
To list collection names present in a MongoDB database,
- Create a client to the MongoDB instance.
- Using the client, and select a database. It returns a reference to the database.
- Call the function list_collection_names() on the database.
- The functions returns an iterator, use for loop to iterate through the list of collections.
Example 1: Get list of MongoDB Collections
Following is an example Python program to list the collection names present in a MongoDB Database.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
#use database "organisation"
mydb = myclient['organisation']
print("List of collections\n--------------------")
#list the collections
for coll in mydb.list_collection_names():
print(coll)
Run Output
