Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot drop collection in mongodb with two dashes in the name

Tags:

mongodb

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.

like image 974
DoodleKana Avatar asked Dec 26 '22 06:12

DoodleKana


1 Answers

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
like image 83
John Petrone Avatar answered Jan 31 '23 03:01

John Petrone