How To Delete a Document in MongoDB?

We often are required to delete the data that we insert into our database.

Deleting a record or a document in MongoDB is a very common operation that involves removing data from the database. In MongoDB, you can use two methods to delete the record. These are deleteOne() or deleteMany() methods. We can use either of them depending on whether we want to delete a single document or multiple documents from the database that match our criteria.

deleteOne() Method:  This method is used when we want to delete a single document from the database. This method takes a query parameter and matches it with all the subsets and whichever record has the same subset value as passed in the query, it gets deleted. 

You can add more optional parameters in the options attributes in this method.

await List.deleteOne({task: "hello"});
Code language: CSS (css)

In the above line of code, List is the name of the database. The parameter passed here is the “task” parameter. Mongodb will delete the document whose “task” subset is equal to “hello”. We can pass another parameter separated with the comma that will be considered as the second parameter.

deleteMany() Method: This method is used when we want to delete more than one document from the database. This function also takes the query parameter and deletes all the records that match with the query passed.

const query = { title: { $regex: "Hello" } }; const result = await List.deleteMany(query);
Code language: PHP (php)

In the above code, all the documents where the title subset contains “Hello”, will be deleted.

findOneandDelete()

This is another method to delete the document. It works the same way as the deleteOne() method. The only difference is that this method returns the details of the deleted document. Therefore this method is used when we want to know the deleted document’s data.

let deletedtask =  await List.findOneAndDelete({task: “hello”}); console.log(deletedtask);
Code language: JavaScript (javascript)

In this example, the method used first finds the record that matches the query, returns the details of that document, and deletes it.

Conclusion

With this information, you can delete the documents from your MongoDB and use the appropriate methods to do so. This blog talks about 3 deleting methods and all are important to know provided with the specific requirements. You can use the deleteOne() method if a single document is to be deleted or the deleteMany() method if multiple documents are to be deleted.  

If you want to delete and want the details of the deleted document, you should use findOneAndDelete(). Hope you got a fair idea on the same.


Posted

in

by

Recent Post

  • What is Knowledge Distillation? Simplifying Complex Models for Faster Inference

    As AI models grow increasingly complex, deploying them in real-time applications becomes challenging due to their computational demands. Knowledge Distillation (KD) offers a solution by transferring knowledge from a large, complex model (the “teacher”) to a smaller, more efficient model (the “student”). This technique allows for significant reductions in model size and computational load without […]

  • Priority Queue in Data Structures: Characteristics, Types, and C Implementation Guide

    In the realm of data structures, a priority queue stands as an advanced extension of the conventional queue. It is an abstract data type that holds a collection of items, each with an associated priority. Unlike a regular queue that dequeues elements in the order of their insertion (following the first-in, first-out principle), a priority […]

  • SRE vs. DevOps: Key Differences and How They Work Together

    In the evolving landscape of software development, businesses are increasingly focusing on speed, reliability, and efficiency. Two methodologies, Site Reliability Engineering (SRE) and DevOps, have gained prominence for their ability to accelerate product releases while improving system stability. While both methodologies share common goals, they differ in focus, responsibilities, and execution. Rather than being seen […]

  • Moving Beyond Traditional Chatbots: Autonomous Agents Redefining Business Operations

    What if your business could operate on autopilot, with AI systems making crucial decisions and managing tasks in real time? Imagine autonomous agents—advanced AI systems capable of making decisions and performing tasks without constant human oversight—transforming your operations. From streamlining workflows to performing seamless customer interactions, these smart agents promise to redefine efficiency and innovation.  […]

  • Mastering Large Action Models: Unleashing Potential and Navigating Complex Challenges in AI

    Imagine an AI assistant that doesn’t just follow commands but anticipates your needs, makes decisions for you, and carries out tasks autonomously. This is the promise of Large Action Models (LAMs), a revolutionary step beyond current AI capabilities. Unlike traditional AI, which reacts to commands, LAMs can think ahead and manage complex scenarios without human […]

  • Harnessing Multimodal AI: A Comprehensive Guide to the Future of Data-Driven Decision Making

    Artificial Intelligence (AI) has been evolving at an astonishing pace, pushing the boundaries of what machines can achieve. Traditionally, AI systems handles single-modal inputs—meaning they could process one type of data at a time, such as text, images, or audio. However, the recent advancements in AI have brought us into the age of multimodal AI, […]

Click to Copy