我这里运行的查询产生的蒙戈shell界面空白(无号,基本没什么):为什么未设置的查询不删除mongoDB中的指定字段?
> db.classrooms.update({ "c_type" : { $exists : true } }, { $unset : { "c_type" : 1 } }, false, true);
另外,我检查了一下应该有c_type删除集合行,但它们仍然存在。
我基本上试图用unset命令删除我的集合中的列/字段。我的语法有什么问题吗?
谢谢!
我这里运行的查询产生的蒙戈shell界面空白(无号,基本没什么):为什么未设置的查询不删除mongoDB中的指定字段?
> db.classrooms.update({ "c_type" : { $exists : true } }, { $unset : { "c_type" : 1 } }, false, true);
另外,我检查了一下应该有c_type删除集合行,但它们仍然存在。
我基本上试图用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_type值以及嵌入式数组和文档。当你运行更新时,你确定你是在'db'吗?你使用的是什么版本的MongoDB? – Stennie 2012-08-12 23:21:26