How to find names of all collections using PyMongo?

Damir picture Damir · Mar 21, 2012 · Viewed 90.6k times · Source

How to find names of all collections using PyMongo and find all fields in chosen collection ? I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection)

Answer

Dogbert picture Dogbert · Mar 21, 2012

To find the collections, you can use collection_names() - http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names

Edit:

The collection_names is deprecated from 3.7 onwards and been replaced by list_collection_names - https://api.mongodb.com/python/current/api/pymongo/database.html#pymongo.database.Database.list_collection_names