Delete a collection from MongoDB with special characters?

MongoDBDatabaseBig Data Analytics

In order to delete the collection which has some special characters like _ or -, you need to use the following syntax −

db.getCollection("yourCollectionName").drop();

To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.createCollection("_personalInformation");
{ "ok" : 1 }

> db.getCollection('_personalInformation').insertOne({"ClientName":"Chris","ClientCountryName":"US"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"Mike","ClientCountryName":"UK"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"David","ClientCountryName":"AUS"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.getCollection('_personalInformation').find().pretty();

The following is the output −

{
   "_id" : ObjectId("5c9158bb4afe5c1d2279d6b2"),
   "ClientName" : "Chris",
   "ClientCountryName" : "US"
}
{
   "_id" : ObjectId("5c9158c84afe5c1d2279d6b3"),
   "ClientName" : "Mike",
   "ClientCountryName" : "UK"
}
{
   "_id" : ObjectId("5c9158d54afe5c1d2279d6b4"),
   "ClientName" : "David",
   "ClientCountryName" : "AUS"
}

Here is the query to delete the collection from MongoDB which has a special character −

> db.getCollection("_personalInformation").drop();

The following is the output −

True

The result TRUE states that we have deleted the collection from MongoDB completely.

raja
Published on 01-Apr-2019 09:52:30
Advertisements