w3resource

Remove document from collection in MongoDB with remove()

Description

We have already learned how to insert records into a collection and also learned how to update the document within a collection based on certain criteria. In this page, we are going to discuss how to remove the document and entire collection.

remove() is used to remove a document and a collection. It may be a good practice if you execute a find() before removing a document as you can fix criteria for removing the document.

Our database name is 'myinfo' and our collection name is 'userdetails'. Here, inserting one more record.

> document=({"user_id" : "testuser","password" :"testpassword" ,"date_of_join" : "16/10/2010" ,"education" :"M.C.A." , "profession" : "CONSULTANT","interest" : "MUSIC","community_name" :["MODERN MUSIC", "CLASSICAL MUSIC","WESTERN MUSIC"],"community_moder_id" : ["MR. BBB","MR. JJJ","MR MMM"],"community_members" : [500,200,1500],"friends_id" : ["MMM123","NNN123","OOO123"],"ban_friends_id" :["BAN123","BAN456","BAN789"]});
> db.userdetails.insert(document) 

View the inserted data into the collection

>db.userdetails.find().pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

{
        "user_id" : "MNOPBWN",
        "password" : "MNOPBWN",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. BBB",
                "MR. JJJ",
                "MR MMM"
        ],
        "community_members" : [
                500,
                200,
                1500
        ],
        "friends_id" : [
                "MMM123",
                "NNN123",
                "OOO123"
        ],
        "ban_friends_id" : [
                "BAN123",
                "BAN456",
                "BAN789"
        ]
}
{
        "user_id" : "QRSTBWN",
        "password" : "NEWPASSWORD",
        "date_of_join" : "17/10/2010",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. BBB",
                "MR. JJJ",
                "MR MMM"
        ],
        "community_members" : [
                500,
                200,
                1500
        ],
        "friends_id" : [
                "MMM123",
                "NNN123",
                "OOO123"
        ],
        "ban_friends_id" : [
                "BAN123",
                "BAN456",
                "BAN789"
        ]
}
{
        "user_id" : "testuser",
        "password" : "testpassword",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. BBB",
                "MR. JJJ",
                "MR MMM"
        ],
        "community_members" : [
                500,
                200,
                1500
        ],
        "friends_id" : [
                "MMM123",
                "NNN123",
                "OOO123"
        ],
        "ban_friends_id" : [
                "BAN123",
                "BAN456",
                "BAN789"
        ]
}

Document written in command prompt

Remove document with remove()

If we want to remove the document from the collection 'userdetails' which contains the userid "testuser" the following mongodb command can be used :

>db.userdetails.remove( { "user_id" : "testuser" } )

Remove all data from a collection

If we want to remove all data from the collection 'userdetails' the following mongodb command can be used :

>db.userdetails.remove({}) 

Remove a collection using drop()

If we want to remove the entire 'userdetails' collection, including all of its documents the following mongodb command can be used :

>db.userdetails.drop()

Output:

> db.userdetails.drop();
true

The drop() returns either true or false. The above function returns true, it means that the operation has completed successfully.

Remove an entire Database using dropDatabase()

If we want to remove an entire database the following mongodb command can be used:

>db.dropDatabase()

It should be a good habit if you use the db command in mongodb prompt to know which database is currently working and you can be sure before removing the database.

> db
myinfo
>db.dropDatabase();  
{ "dropped" : "myinfo", "ok" : 1 }

Previous: MongoDB UPDATE
Next: MongoDB Indexes Intorduction



Follow us on Facebook and Twitter for latest update.