2012-08-12 68 views
0

我这里运行的查询产生的蒙戈shell界面空白(无号,基本没什么):为什么未设置的查询不删除mongoDB中的指定字段?

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

另外,我检查了一下应该有c_type删除集合行,但它们仍然存在。

我基本上试图用unset命令删除我的集合中的列/字段。我的语法有什么问题吗?

谢谢!

+3

您的语法看起来不错;事实上,你的例子似乎为我工作得很好:)。我尝试了简单的c_type值以及嵌入式数组和文档。当你运行更新时,你确定你是在'db'吗?你使用的是什么版本的MongoDB? – Stennie 2012-08-12 23:21:26

回答

1

而且我的意见,这是我测试的例子:

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" } 
0

想通了。基本上我不得不双重报价$exists$unset

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

您使用的是哪个版本的MongoDB? – Stennie 2012-08-14 21:38:58

+0

我如何发现?当我去壳体它显示'MongoDB外壳版本:2.0.4' – Goalie 2012-08-14 21:42:03

+0

尝试:'db.serverStatus()。version' – Stennie 2012-08-15 01:14:53

相关问题