PyMongo – How to Get Collection Names of MongoDB Database?

List Collection Names of MongoDB Database

To list collection names present in a MongoDB database,

  1. Create a client to the MongoDB instance.
  2. Using the client, and select a database. It returns a reference to the database.
  3. Call the function list_collection_names() on the database.
  4. 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

PyMongo List Collection Names in Database