You can check if a collection exists in MongoDB using the list_collection_names() method of a MongoClient object.
Here's an example:
from pymongo import MongoClient
# create a MongoClient object
client = MongoClient()
# check if a collection called "mycollection" exists
mydb = client["mydatabase"]
if "mycollection" in mydb.list_collection_names():
print("The collection exists.")
else:
print("The collection does not exist.")
In this example, we check if a collection called "mycollection" exists in the "mydatabase" database by calling the list_collection_names() method of the MongoClient object and checking if the collection name is in the returned list.