Lecture Notes Of Day 8: MongoDB CRUD Operations – Part 4 (Delete)

Rashmi Mishra
0

 

Lecture Notes Of Day 8: MongoDB CRUD Operations – Part 4 (Delete)


Objective:

Learn how to delete documents from a MongoDB collection using deleteOne() and deleteMany() methods.

Outcome:

By the end of the lecture, students will:

  • Understand how to delete a single document using deleteOne().
  • Learn how to delete multiple documents using deleteMany().
  • Be able to write queries for different deletion scenarios.

1. Introduction to Deleting Documents

Deleting documents in MongoDB is an essential operation in managing data. MongoDB provides two methods for deletion:

  • deleteOne(): Deletes a single document that matches the specified filter.
  • deleteMany(): Deletes all documents that match the specified filter.

2. Syntax and Explanation

a. deleteOne()

The deleteOne() method removes the first document that matches the query criteria.

Syntax:

javascript

CopyEdit

db.collection.deleteOne(filter)

  • filter: A query object specifying the condition to match the document to be deleted.

Example:

javascript

CopyEdit

db.students.deleteOne({ name: "John Doe" })

This query will delete the first document in the students collection where the name field is "John Doe".


b. deleteMany()

The deleteMany() method removes all documents that match the query criteria.

Syntax:

javascript

CopyEdit

db.collection.deleteMany(filter)

  • filter: A query object specifying the condition to match documents to be deleted.

Example:

javascript

CopyEdit

db.students.deleteMany({ grade: "C" })

This query will delete all documents in the students collection where the grade field is "C".


3. Examples with Explanations

a. Deleting a Single Document

Scenario: Remove a student named "Alice" from the students collection.

javascript

CopyEdit

db.students.deleteOne({ name: "Alice" })

  • This command searches for the first document where the name is "Alice" and deletes it.
  • If multiple documents match, only the first occurrence will be removed.

b. Deleting Multiple Documents

Scenario: Remove all students who scored less than 50 marks.

javascript

CopyEdit

db.students.deleteMany({ marks: { $lt: 50 } })

  • The filter { marks: { $lt: 50 } } matches all documents where the marks field is less than 50.
  • All such documents are removed from the collection.

4. Checking the Deletion Result

Both deleteOne() and deleteMany() return a result object that contains information about the deletion operation.

Example Output:

javascript

CopyEdit

{

  "acknowledged": true,

  "deletedCount": 1

}

  • acknowledged: Indicates if the operation was acknowledged by the server.
  • deletedCount: Shows the number of documents deleted.

5. Deleting All Documents in a Collection

To remove all documents in a collection, pass an empty filter {} to the deleteMany() method.

Example:

javascript

CopyEdit

db.students.deleteMany({})

This deletes all documents in the students collection. Use this operation cautiously.


6. Practical Tips for Safe Deletion

1.  Test Your Query First: Before running a delete operation, test your query using find() to ensure it matches the intended documents.

javascript

CopyEdit

db.students.find({ grade: "C" })

2.  Backup Your Data: Always take a backup of your database before performing bulk delete operations.

3.  Use Filters Wisely: Ensure your filter criteria are specific to avoid unintended deletions.


7. Hands-On Practice

Task 1: Delete a Single Document

  • Remove a document where the name is "Michael".

Task 2: Delete Multiple Documents

  • Remove all documents where the course is "History".

Task 3: Delete All Documents

  • Clear the temporary_data collection by removing all its documents.

8. Summary

  • The deleteOne() method deletes the first document that matches the filter criteria.
  • The deleteMany() method deletes all documents that match the filter criteria.
  • Always test your query and take necessary precautions before performing delete operations.

9. Assignment

1.  Write a query to delete all documents where the age is less than 18 in the users collection.

2.  Experiment with deleting documents in a test collection and note the results.

Note: Encourage students to test each query using the find() method to verify the conditions match the intended documents before performing deletions.


Tags

Post a Comment

0Comments

Post a Comment (0)

About Me