Assignments Of Day 8: MongoDB CRUD Operations – Part 4 (Delete)
Assignment 1: Delete a Document by Name
Task:
Delete a student named "Emma" from the students collection.
Solution:
javascript
CopyEdit
db.students.deleteOne({ name: "Emma" })
Explanation:
- The filter { name: "Emma" } matches the first document where the name field equals "Emma".
- The deleteOne() method removes this document from the collection.
Assignment 2: Delete Documents by Age
Task:
Delete all students who are 20 years old from the students collection.
Solution:
javascript
CopyEdit
db.students.deleteMany({ age: 20 })
Explanation:
- The filter { age: 20 } matches all documents where the age field is 20.
- The deleteMany() method removes all matching documents.
Assignment 3: Delete Documents with Marks Below a Threshold
Task:
Delete all students who scored less than 40 marks in the students collection.
Solution:
javascript
CopyEdit
db.students.deleteMany({ marks: { $lt: 40 } })
Explanation:
- The filter { marks: { $lt: 40 } } uses the $lt operator to match documents where the marks field is less than 40.
- The deleteMany() method removes all such documents.
Assignment 4: Delete Documents Based on Multiple Conditions
Task:
Delete all students who are 18 years old and enrolled in the "Math" course.
Solution:
javascript
CopyEdit
db.students.deleteMany({ age: 18, course: "Math" })
Explanation:
- The filter { age: 18, course: "Math" } matches documents where both conditions are true.
- The deleteMany() method removes all documents meeting these criteria.
Assignment 5: Delete Documents with Specific IDs
Task:
Delete the document with the _id of ObjectId("64a7c5f8e9d1a74c12345678").
Solution:
javascript
CopyEdit
db.students.deleteOne({ _id: ObjectId("64a7c5f8e9d1a74c12345678") })
Explanation:
- The _id field is a unique identifier, and we use it to target a specific document.
- The ObjectId() function ensures the ID is correctly formatted for the query.
Assignment 6: Delete All Documents in a Collection
Task:
Clear all records in the temporary_data collection.
Solution:
javascript
CopyEdit
db.temporary_data.deleteMany({})
Explanation:
- An empty filter {} matches all documents in the collection.
- The deleteMany() method deletes every document.
Assignment 7: Delete Documents with Embedded Fields
Task:
Delete all students with an address field where the city is "New York."
Solution:
javascript
CopyEdit
db.students.deleteMany({ "address.city": "New York" })
Explanation:
- The filter "address.city": "New York" accesses the city field inside the address embedded document.
- The deleteMany() method removes all documents where this condition is met.
Assignment 8: Delete Documents Using a Range Condition
Task:
Delete all students whose ages are between 15 and 20 (inclusive).
Solution:
javascript
CopyEdit
db.students.deleteMany({ age: { $gte: 15, $lte: 20 } })
Explanation:
- The filter { age: { $gte: 15, $lte: 20 } } uses the $gte (greater than or equal to) and $lte (less than or equal to) operators to match the range.
- The deleteMany() method removes all documents satisfying this range condition.
Assignment 9: Delete Documents with a Field Missing
Task:
Delete all students where the phone field does not exist.
Solution:
javascript
CopyEdit
db.students.deleteMany({ phone: { $exists: false } })
Explanation:
- The filter { phone: { $exists: false } } matches documents where the phone field is missing.
- The deleteMany() method removes all such documents.
Assignment 10: Verify Deletion Using Result
Task:
Delete all students in the "Physics" course and print the number of documents deleted.
Solution:
javascript
CopyEdit
const result = db.students.deleteMany({ course: "Physics" });
print(`Documents deleted: ${result.deletedCount}`);
Explanation:
1. The filter { course: "Physics" } matches all documents where the course field equals "Physics".
2. The deleteMany() method removes these documents and returns a result object.
3. The deletedCount field in the result object shows the number of documents deleted.
Summary Table
Task | Method | Filter Example |
Delete a document by name | deleteOne | { name: "Emma" } |
Delete documents by age | deleteMany | { age: 20 } |
Delete documents with marks below 40 | deleteMany | { marks: { $lt: 40 } } |
Delete documents with multiple conditions | deleteMany | { age: 18, course: "Math" } |
Delete by _id | deleteOne | { _id: ObjectId("id_value") } |
Delete all documents in a collection | deleteMany | {} |
Delete documents with embedded fields | deleteMany | { "address.city": "New York" } |
Delete documents by range | deleteMany | { age: { $gte: 15, $lte: 20 } } |
Delete documents with missing fields | deleteMany | { phone: { $exists: false } } |
Verify deletion count | deleteMany | { course: "Physics" } |
