The main difference is that db.remove({}) removes all documents but your collection still have metadata like indexes. The dropDatabase remove literally the entire collection including metadata.
If you are recreating collection indexes or you don't have it please use dropDatabase it's much faster.
Check this link to understand db.collection.drop() vs db.dropDatabase()
A benefit of simply dropping a collection is that it is much faster than removing all of a collection's documents. If your collection will be "re-created immediately" anyway (assuming that includes index re-creation), then this is probably the most-attractive option.
The main difference is that
db.remove({})
removes all documents but your collection still have metadata like indexes. ThedropDatabase
remove literally the entire collection including metadata.If you are recreating collection indexes or you don't have it please use
dropDatabase
it's much faster.Check this link to understand
db.collection.drop()
vsdb.dropDatabase()
If you are into learning MongoDb I recommend reading this book. It also explains your use case.
A benefit of simply dropping a collection is that it is much faster than removing all of a collection's documents. If your collection will be "re-created immediately" anyway (assuming that includes index re-creation), then this is probably the most-attractive option.
The authors of the book MongoDB: The Definitive Guide (Kristina Chodorow and Michael Dirolf) ran an experiment where they provided a Python script which timed a
drop
vs. aremove
of 1000000 records. The results came in at 0.01 seconds for thedrop
and 46.08 seconds for theremove
. Now while the exact times may differ based-on hardware and other factors, it nonetheless illustrates the point that thedrop
is significantly faster.reference: Chodorow K., Dirolf M. (2010). “MongoDB: The Definitive Guide.” O'Reilly Media, Inc. Sebastapol, CA., pp.25