Database IntegrationLesson 67Free preview
MongoDB Drop Collection
This lesson is part of Node.js · All levels
Summary
This lesson explains how to delete MongoDB collection entirely by drop() or db.dropCollection(name). Drop deletes the entire document, index, and definition collection in one step, so much faster than deleteMany. You also deal with errors that occur when the collection doesn't exist.
Key points
- Collection.drop() or db.dropCollection(name) deleted the permanent collection.
- Drop discards all indexes along with its documents (as opposed to deleteMany which maintains the index).
- Running a drop on a collection that doesn't exist produces error code 26 (NSNotFound).
- After the drop, a new collection is automatically re-formed when the next insert is made.
- Drop is not reversible; make sure to back up or confirm before running it.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.