为什么unset查询不删除mongoDB中的指定字段?

为什么unset查询不删除mongoDB中的指定字段?,mongodb,Mongodb,我在这里运行的查询在mongo shell界面中生成一个空白(没有数字,基本上什么都没有): > db.classrooms.update( { "c_type" : { $exists : true } }, { $unset : { "c_type" : 1 } }, false, true); 此外,我还检查了应该删除c_类型的集合行,但它们仍然存在 我基本上是在尝试使用unset命令删除集合中的列/字段。我的语法有问题吗 谢谢大家! 关于我的评论,下面是我测试的示例: Mongo

我在这里运行的查询在mongo shell界面中生成一个空白(没有数字,基本上什么都没有):

> db.classrooms.update( { "c_type" : { $exists : true } }, { $unset : { "c_type" : 1 } }, false, true);
此外,我还检查了应该删除c_类型的集合行,但它们仍然存在

我基本上是在尝试使用unset命令删除集合中的列/字段。我的语法有问题吗


谢谢大家!

关于我的评论,下面是我测试的示例:

MongoDB shell version: 2.0.6
connecting to: test

> db.classrooms.insert({"example": "field", "c_type" : "Open"});
> db.classrooms.insert({"example": "array", "c_type" : ['Available']});
> db.classrooms.insert({"example": "obj",   "c_type" : {'Booked' : 'Yes'}});

> db.classrooms.find()
{
    "_id" : ObjectId("502abd4a332f362f58906683"),
    "example" : "field",
    "c_type" : "Open"
}
{
    "_id" : ObjectId("502abd4e332f362f58906684"),
    "example" : "array",
    "c_type" : [
        "Available"
    ]
}
{
    "_id" : ObjectId("502abd53332f362f58906685"),
    "example" : "obj",
    "c_type" : {
        "Booked" : "Yes"
    }
}

> db.classrooms.update(
   { "c_type" : { $exists : true } },
   { $unset : { "c_type" : 1 } }, 
   false,  // upsert
   true);  // update multiple records

> db.classrooms.find()
{ "_id" : ObjectId("502abd4a332f362f58906683"), "example" : "field" }
{ "_id" : ObjectId("502abd4e332f362f58906684"), "example" : "array" }
{ "_id" : ObjectId("502abd53332f362f58906685"), "example" : "obj" }

我明白了。基本上,我必须双引号
$exists
$unset

> db.classrooms.update( { "c_type" : { "$exists" : true } }, { "$unset" : { "c_type" : 1 } }, false, true);

你的语法似乎很好;事实上,你的例子对我来说似乎很好:)。我尝试使用c_类型的简单值以及嵌入式数组和文档。运行更新时,您确定自己处于正确的
db
?您使用的是哪个版本的MongoDB?您使用的是哪个版本的MongoDB?我怎么知道?当我转到shell时,它会显示
MongoDB shell版本:2.0.4
Try:
db.serverStatus().Version
返回的版本是2.0.4Hrm。。我在MongoDB 2.0.4 shell(OS X)上也尝试了同样的方法,但无法重现您的错误。在MongoDB操作符周围剪切并粘贴update()而不使用双引号,效果很好。