How can I drop a collection in MongoDB with two dashes in the name?



Let us first see the syntax to drop a collection −

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

For demo, we will create a collection name with two dashes as shown below −

> db.createCollection("company--EmployeeInformation"); { "ok" : 1 }

Create the above collection “company--EmployeeInformation “ with documents. Following is the query:

>db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Amazon","EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c5ff6d78f205348bc654") } >db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Google","EmployeeName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c60b6d78f205348bc655") }

Following is the query to display all documents from a collection with the help of find() method −

> db.getCollection("company--EmployeeInformation").find();

This will produce the following output −

{ "_id" : ObjectId("5cd7c5ff6d78f205348bc654"), "CompanyName" : "Amazon", "EmployeeName" : "Chris" } { "_id" : ObjectId("5cd7c60b6d78f205348bc655"), "CompanyName" : "Google", "EmployeeName" : "Robert" }

Let us now drop a collection in MongoDB with two dashes in the name −

> db.getCollection("company--EmployeeInformation").drop();

This will produce the following output −

True

Look at the above output, we have dropped the collection name with two dashes.

Updated on: 2019-07-30T22:30:26+05:30

450 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements