How to find names of all collections using PyMongo?

后端 未结 5 1762
遇见更好的自我
遇见更好的自我 2021-02-01 00:08

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

5条回答
  •  春和景丽
    2021-02-01 00:56

    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

提交回复
热议问题