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
More of Mongodb
- How to check the version of MongoDB?
- How to use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...