37

How to check in PyMongo if collection exists and if exists empty (remove all from collection)? I have tried like

collection.remove()

or

collection.remove({})

but it doesn't delete collection. How to do that ?

3 Answers 3

72

Sample code in Pymongo with comment as explanation:

from pymongo import MongoClient
connection = MongoClient('localhost', 27017) #Connect to mongodb

print(connection.database_names())  #Return a list of db, equal to: > show dbs

db = connection['testdb1']          #equal to: > use testdb1
print(db.list_collection_names())        #Return a list of collections in 'testdb1'
print("posts" in db.list_collection_names())     #Check if collection "posts" 
                                            #  exists in db (testdb1)

collection = db['posts']
print(collection.count() == 0)    #Check if collection named 'posts' is empty

collection.drop()                 #Delete(drop) collection named 'posts' from db and all documents contained. 
3
  • do you really want to be querying all collections in the database every time you want to check if the collection exists? Commented Feb 19, 2018 at 9:48
  • 1
    @MoatazElmasry, method for checking if collection exists has not yet been implemented in mongodb, you can check on this issue: jira.mongodb.org/browse/SERVER-1938 Commented Feb 25, 2018 at 3:42
  • @MoatazElmasry meanwhile, if you really need to optimize the performance of you application, you can resort to other techniques such as pre-caching collection results. Commented Feb 25, 2018 at 3:45
21

You should use .drop() instead of .remove(), see documentation for detail: http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.drop

=====

Sorry for misunderstanding your question.

To check if a collection exists, use method collection_names on database:

>>> collection_name in database.list_collection_names()

To check if a collection is empty, use:

>>> collection.count() == 0

both will return True or False in result.

3
  • 1
    collection_names is deprecated. Use list_collection_names instead Commented Oct 3, 2019 at 10:03
  • this works. it's scary this will drop everything without asking for confirmation...
    – citynorman
    Commented Apr 14, 2022 at 2:31
  • @citynorman Well, it's an API not CLI, assuming you understand what you are doing, same in other DSLs like MySQL's DROP TABLE.
    – Reorx
    Commented Apr 14, 2022 at 5:27
5

Have you tried this:

db.collection.remove();

1
  • db.collection.remove() only removes all records from collection that matches the query given in remove. Eg db.collection.remove({name: 'abc'}) will remove all records with name 'abc', empty query in remove will cause all records to be removed, but collection will stay as it is.
    – Dania
    Commented Feb 12, 2018 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.