0
0
MongoDBquery~30 mins

Delete with filter conditions in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Delete Documents with Filter Conditions in MongoDB
📖 Scenario: You manage a small online bookstore database. Over time, some books become outdated or discontinued. You want to clean up the database by deleting books that meet certain conditions.
🎯 Goal: Build a MongoDB query to delete documents from the books collection using filter conditions.
📋 What You'll Learn
Create a books collection with specific book documents
Add a filter condition variable to select books published before 2010
Write a delete query using the filter condition to remove old books
Confirm the delete query targets the correct documents
💡 Why This Matters
🌍 Real World
Cleaning up outdated or irrelevant data in a database to keep it accurate and efficient.
💼 Career
Database administrators and developers often need to delete records based on conditions to maintain data quality.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with these exact documents: { title: 'Learn MongoDB', year: 2015 }, { title: 'Old Book', year: 2005 }, { title: 'Modern Database', year: 2020 }
MongoDB
Need a hint?

Use an array of objects with the exact titles and years given.

2
Create a filter condition for books published before 2010
Create a variable called filterCondition and set it to { year: { $lt: 2010 } } to select books published before 2010
MongoDB
Need a hint?

Use MongoDB's $lt operator to filter years less than 2010.

3
Write the delete query using the filter condition
Write a line that calls db.books.deleteMany(filterCondition) to delete all books matching the filter condition
MongoDB
Need a hint?

Use deleteMany with the filter condition variable.

4
Confirm the delete query targets the correct documents
Add a line that calls db.books.find() to check remaining documents after deletion
MongoDB
Need a hint?

Use find() to see which documents remain after deletion.