我在这里运行的查询在 mongo shell 接口中生成一个空白(没有数字,基本上什么都没有):
> 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);