0
0
MongoDBquery~15 mins

Delete all documents in collection in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Delete All Documents in a MongoDB Collection
📖 Scenario: You are managing a MongoDB database for a small online bookstore. Sometimes, you need to clear out all the book records to reset the collection before adding new data.
🎯 Goal: Learn how to delete all documents from a MongoDB collection using a simple command.
📋 What You'll Learn
Create a MongoDB collection named books with some sample documents.
Set up a variable to reference the books collection.
Use the correct MongoDB command to delete all documents in the books collection.
Confirm the deletion command is correctly structured.
💡 Why This Matters
🌍 Real World
Clearing out old or test data from a MongoDB collection is common when resetting databases or cleaning up after tests.
💼 Career
Database administrators and backend developers often need to delete data efficiently and safely using commands like 'deleteMany'.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a MongoDB collection called books and insert these exact documents: { title: "Book A", author: "Author 1" }, { title: "Book B", author: "Author 2" }, and { title: "Book C", author: "Author 3" }.
MongoDB
Need a hint?

Use db.books.insertMany([...]) to add multiple documents at once.

2
Set a variable to reference the books collection
Create a variable called collection and set it to db.books to reference the books collection.
MongoDB
Need a hint?

Use const collection = db.books to create the variable.

3
Delete all documents from the books collection
Use the deleteMany method on the collection variable with an empty filter {} to delete all documents in the collection.
MongoDB
Need a hint?

Use collection.deleteMany({}) to remove all documents.

4
Confirm the deletion command is complete
Add a semicolon ; at the end of the collection.deleteMany({}) command to complete the statement.
MongoDB
Need a hint?

Remember to end statements with a semicolon ; in JavaScript.