mongodb在更新中设置为null [英] mongodb set null in update
本文介绍了mongodb在更新中设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须交换文档的值.
I have to interchange the values of a document.
var query = {"_id" : ObjectId("53e1c254382f891cc600076d")};
db.properties.find(query).forEach(function(prop){
printjson({"_id":prop._id, "val":prop.val, "ua":prop.ua});
db.properties.update(query, {$set:{ua: prop.val}},{$unset:{val:""}});
});
在进行更新操作之前,文档如下所示:
Before update operation, document looks like this:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : 9876541,
"ua" : null
}
更新后,它变为:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : 9876541,
"ua" : 9876541
}
但是我希望它是:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : null,
"ua" : 9876541
}
但是它不起作用.还直接将"val"
设置为({$set:{val:null}})
会删除我的整个文档.
But its not working. also setting "val"
null ({$set:{val:null}})
directly it deleted my entire document.
推荐答案
设置undefined
,它的工作原理就像是一种魅力!
Setting undefined
, It worked like a charm!!
db.properties.find(query).forEach(function(prop){
db.properties.update(query, {$set:{ua: prop.val, val:undefined}});
});
这篇关于mongodb在更新中设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文