Drop Collection if already exists in MongoDB using Python
Using drop() 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:
Python3
import pymongo
db = client[ "mydatabase" ]
col = db[ "gfg" ]
print (col.drop())
|
Output:
Example 2: If collection does not exist.
Python3
import pymongo
db = client[ "mydatabase" ]
col = db[ "gfg" ]
if col.drop():
print ( 'Deleted' )
else :
print ( 'Not Present' )
|
Output:
Not Present
Last Updated :
14 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...