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
DeprecationWarning: collection_names is deprecated. Use list_collection_names instead.
Changed in version 3.7: Deprecated. Use list_collection_names() instead.
An example to read the database name from user input and then finding the list collection names would be:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
dbname = input("Enter database name: ")
mydb = myclient[dbname]
#list the collections
for coll in mydb.list_collection_names():
print(coll)
Reference: Python MongoDB