mongodb

3 Best Ways to DROP Remove MongoDB Collection [DROP]

“How to “DROP” collection from database MongoDB”?

In this section, I am going to describes “DROP” or DELETE collection in MongoDB. Below version 3.4, does not provide any user interface like SQL Server but MongoDB 3.4 solve this problem with help of “Robomongo Management Tool”.

As per my understanding, it is a great achievement of MongoDB to provide us “Robomongo Management Tool”.

You can 
download Robomongo MongoDB tool and install Robomongo in your machine and start to Create, Rename and DROP MongoDB database, collections, functions and Users.

In the MongoDB, the command “db.collection.drop()” is used to DROP a database collection.

Syntax: - [ db.collection_name.drop() ]

The “DROP” method will returns true, if successfully dropped! Otherwise return false!

3 Ways to “DROP” MongoDB Database:-
1.      “Use” of Robomongo Management Tool
2.      “Use” Command Line.
3.      “Use” ASP.Net C# Code.
Here you can decide to “DROP MongoDb collection”, “database” and “users”.

“DROP” MongoDB Collection Using Robomongo Tool:-
You should go (download URL: https://robomongo.org/) and install user interface. It’s so easy to install and start MongoDB developments.

Examples,

“DROP” MongoDB Collection Using Command:-
DROP a MongoDB Collection using command in the following steps,
1.       “Use” the Database Name.
2.       Execute the command “db.collection_name.drop() database.
3.       Finally, you can verify using the “show collections” command.

Examples,
> use demoDB
switched to db demoDB

> db.getCollection('users').find({})
{ "_id" : ObjectId("5857b7199244b5a6e9946d2c"), "Name" : "Anil Singh", "Age" : 30 }

> show collections
users

> db.users.drop()
true

Using ASP.NET C# (MVC) to “DROP” MongoDB Collection,
public ActionResult Index()
{
    MongoClient client = new MongoClient();
    //GET MONGO SERVER.
    var server = client.GetServer();

    //GET MONGO DATABASE.
    var db = server.GetDatabase("mydb");

    //GET MONGO COLLECTION.
    var collection =   db.GetCollection("users");

    //ADD COLLECTION ROWS.
    collection.Save(new user {name ="Alok", site ="code-view", CretaeDate=DateTime.Now});

    //DROP COLLECTION
    collection.Drop();

    return View();
}

public class user
{
    public ObjectId id { get; set; }
    public string name { get; set; }
    public string site { get; set; }
    public DateTime CretaeDate { get; set; }
}

Reference links,

I hope you are enjoying with this post! Please share with you friends. Thank you!!

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^