DoodleKana
DoodleKana

Reputation: 2206

Cannot drop collection in mongodb with two dashes in the name

I have a collection I created using mongoimport tool from a file. The problem is I had a typo in my command. Now I ended up with a collection that has two dashes in them. mycollection--file (do not forget the space before the dashes) Now mongodb wont let me drop the collection. db.mycollection--file.drop() It will get SyntaxError: Unexpected Identifier. It is not just drop but find or anyway to interact with the collection will get that syntax error. But it does get listed in the db, if you do show collections.

Upvotes: 5

Views: 2304

Answers (1)

John Petrone
John Petrone

Reputation: 27487

I believe this will work for you:

db["mycollection--file"].drop()

The square brackets with quotation marks syntax is useful for collection names that have characters in their names that interfere will normal shell commands.

UPDATE:

I've added, queried and then deleted from this collection name in my own mongodb database:

 var x = {first:1}
 db["mycollection--file"].save(x)
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("53d96289aa812bcf293121df")
})
  db["mycollection--file"].find()
{ "_id" : ObjectId("53d96289aa812bcf293121df"), "first" : 1 }
  db["mycollection--file"].drop()
true

Upvotes: 11

Related Questions