How to Check If a Collection Exist in a Database in Python MongoDB


In this example, we will learn how to check if a collection exists in a database by listing all collections in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient('localhost', 27017)

db = client['mydatabase']

collection = db['us_state']

print(db.list_collection_names())

Output:

['us_state']
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments