我不能确切地告诉你你是如何走到这一步的,但实际上你已经创建了一个名为[object Object] 的集合。这有点做作,但您可以通过以下方式重现您的情况:
// 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 }
因此,MongoDB 允许使用 Object 创建一个集合,但无论您传入哪个对象,所有对象都会评估为相同的 [object Object] 字符串。这意味着您无法确定自己是如何做到的创建这个集合,但从好的方面来说,这也意味着您需要做的就是创建任何对象,您可以使用它来删除它。就我而言,我将只重用上面的 z 变量,但您基本上可以使用任何对象来进行删除:
> db[z].drop()
true
> db.getCollection( "system.namespaces" ).find();
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\")" }
{ "name" : "x.system.indexes" }
{ "name" : "x.ObjectId(\"552b9e7d8c5b893bc6bfae45\").$_id_" }
你有它,它消失了。至于这是否是一个错误,你可以说[object Object] 是一个有效的集合名称——我不建议使用它,但这并不违法。所以也许不是一个错误,而是一个可以提出的改进。
顺便说一句,我测试了你甚至不必在这里实际使用对象,你可以用字符串来做,像这样:
var j = '[object Object]'
db[j].drop()