9951 explained code solutions for 126 technologies


mongodbHow to clear a collection in MongoDB?


To clear a collection in MongoDB, you can use the drop() method. This will delete the collection and all of its documents.

Example

db.collection.drop()

Output example

true

The drop() method takes no parameters and returns true if the collection was dropped successfully.

Code explanation

  • db: This is the database object.
  • collection: This is the name of the collection to be dropped.
  • drop(): This is the method used to drop the collection.

Helpful links

Edit this code on GitHub