MongoDb Tutorial

Day 10 : Delete document in MongoDB








Mongodb tutorial series

Overview

In this part of the Learn Mongo Series, we will learn how to delete a document in mongodb.

Let's start

We can delete a document in mongodb using the remove() command. remove() method in mongodb takes 2 parameters which are explained below :

  • Criteria : We pass the deletion criteria on the basis of which documents will be deleted.
  • Flag : If this flag is 1 or true , then only the first occurrence of the documents matching the deletion criteria is deleted.
Syntax : The syntax for removing document is as follows: db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Removing documents

There are 3 ways to remove the documents using the remove() method which are :

    Consider the following dataset for the operations :
    										
    > use students
    switched to db students
    > db.details.find().pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
            "name" : "nodejsera",
            "age" : "10",
            "description" : "Mongodb tuttorial series"
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
            "name" : "b",
            "age" : "12",
            "description" : "something "
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
            "age" : "11",
            "description" : "learn mongodb",
            "name" : "a"
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
            "name" : "nodejsera",
            "age" : "13",
            "description" : "Express tutorial series"
    }
    >
    
    										
    									

  1. Remove all documents : If we want to remove all the documents from the collection then we can use db.collection_name.remove() command as shown below :
    											
    > use students
    switched to db students
    > db.details.remove()
    >
    
    											
    										

    It will remove all the documents from the details collection.
  2. Remove all documents matching a deletion_Criteria : If we want to remove all the documents which matches a certain criteria, then we can use db.collection_name.remove(Deletion_criteria) command as shown below :
    											
    > use students
    switched to db students
    > db.details.remove({'name':'nodejsera'})
    >  db.details.find().pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
            "name" : "b",
            "age" : "12",
            "description" : "something "
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
            "age" : "11",
            "description" : "learn mongodb",
            "name" : "a"
    }
    >
    
    											
    										

    If you observe, all the documents matching the deletion criteria are removed.
  3. Remove the first occurrence of the documents matching the deletion criteria: If we want to delete the first occurrence of the document matching the deletion criteria , then we can use db.collection_name.remove(Deletion_criteria,1) command as shown below :
    											
    > use students
    switched to db students
    > db.details.remove({'name':'nodejsera'},1)
    >  db.details.find().pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
            "name" : "b",
            "age" : "12",
            "description" : "something "
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
            "age" : "11",
            "description" : "learn mongodb",
            "name" : "a"
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
            "name" : "nodejsera",
            "age" : "13",
            "description" : "Express tutorial series"
    }
    >
    
    											
    										

  4. Hurray !! Mission Accomplished. We have successfully removed the documents from mongodb collections.

Summary

In this part of learn mongo series , we learned about how we can remove documents from mongodb collection. We learned the following commands of mongodb :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. db.Collection_name.find().pretty() : This command is used to list all the documents from the collection
  3. db.Collection_name.remove() : This command is used to remove all the documents from the collection.
  4. db.Collection_name.remove({Deletion_Criteria}) : This command is used to remove all the documents from the collection which matches deletion criteria.
  5. db.Collection_name.remove({Deletion_Criteria},1) : This command is used to remove the first occurrence of the document from the collection whcih macthes deletion criteria.