Usingdrop() method we can drop collection if collection exists. If collection is not found then it returns False otherwise it returns True if collection is dropped.
Syntax:
drop()
Example 1:
The sample database is as follows:
importpymongoclient=pymongo.MongoClient("mongodb://localhost:27017/")# Database namedb=client["mydatabase"]# Collection namecol=db["gfg"]# drop collection col1print(col.drop())
Output:
Example 2: If collection does not exist.
importpymongoclient=pymongo.MongoClient("mongodb://localhost:27017/")# Database namedb=client["mydatabase"]# Collection namecol=db["gfg"]# drop collection col1ifcol.drop():print('Deleted')else:print('Not Present')
Not Present
A