Delete/Remove collection in MongoDB

Ratings:
(4)
Views: 0
Banner-Img
Share this blog:

Remove Collection in MongoDB

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 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 can fix a 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

These core tutorials will help you to 
learn the fundamentals of Mongo DB.
For an in-depth understanding and practical experience, 
explore Online Mongo DB Training.
 

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 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 }

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox