Reputation: 1366
I have a MongoDB with some collections. When typing show collections
along my actual collections I see mysterious [object Object]
collection. I cannot use or delete it cause it's name has bad characters in it.
Can someone explain what may have caused this "collection" to appear and how to delete it?
Update:
db.getCollectionNames()
returns same result:
[ "[object Object]", "my_collection", "system.indexes", "my_collection1" ]
Update2:
db.getCollection("[object Object]").drop()
worked. The cause of such a bug is still unknown
Upvotes: 3
Views: 220
Reputation: 21682
I can't tell you exactly how you got to this point, but what has happened is that you have literally created a collection called [object Object]
. It's a bit contrived, but here's how you can recreate your situation:
// create an object, let's call it y
> var y = {a : 1, b : 2, c : [1, 2, 3]}
// now create a collection using the variable as the name by inserting
> db[y].insert({s : 1})
// we now have [object Object] as a collection
> show collections
ObjectId("552b9e7d8c5b893bc6bfae45")
[object Object]
system.indexes
// This makes it a little more obvious what we have done
> db.getCollection( "system.namespaces" ).find();
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\")" }
{ "name" : "x.system.indexes" }
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\").$_id_" }
{ "name" : "x.[object Object]" }
{ "name" : "x.[object Object].$_id_" }
// We can even query it
> db[y].find()
{ "_id" : ObjectId("552b9f728c5b893bc6bfae47"), "s" : 1 }
// To make it even more obvious what is going on, let's use a different object
> var z = {a : 1, b : 2, c : [1, 2, 3, 4]}
> db[z].insert({t : 1})
// BUT, no new collection this time, we still just have one [object Object]
> db.getCollection( "system.namespaces" ).find();
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\")" }
{ "name" : "x.system.indexes" }
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\").$_id_" }
{ "name" : "x.[object Object]" }
{ "name" : "x.[object Object].$_id_" }
// let's confirm by querying
db[z].find()
{ "_id" : ObjectId("552b9f728c5b893bc6bfae47"), "s" : 1 }
{ "_id" : ObjectId("552ba1888c5b893bc6bfae48"), "t" : 1 }
So, MongoDB is allowing the creation of a collection with an Object, but all objects evaluate to the same [object Object]
string regardless of the object you pass in. That means that you can't really be sure how you managed to create this collection, but on the plus side it also means that all you need to do is create any object and you can use that to remove it. For my case, I will just re-use the z
variable above, but you can essentially use any object to do the removal:
> db[z].drop()
true
> db.getCollection( "system.namespaces" ).find();
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\")" }
{ "name" : "x.system.indexes" }
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\").$_id_" }
And there you have it, it's gone. As to whether this is a bug or not, well you can make the argument that [object Object]
is a valid collection name - I wouldn't recommend using it, but it's not illegal. So perhaps not a bug, but an improvement that could be suggested nonetheless.
As an aside, I tested you don't even have to actually use an object here, you can do it with the string, something like this:
var j = '[object Object]'
db[j].drop()
Upvotes: 3