Mongodb set null in update

I need to change the values ​​of the 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 the update operation, the document is as follows:

{
    "_id" : ObjectId("53e1c254382f891cc600076d"),
    "val" : 9876541,
    "ua" : null
}

And after the update, he refers to:

{
    "_id" : ObjectId("53e1c254382f891cc600076d"),
    "val" : 9876541,
    "ua" : 9876541
}

But I expect this as:

{
    "_id" : ObjectId("53e1c254382f891cc600076d"),
    "val" : null,
    "ua" : 9876541
}

But it does not work. also setting "val"null ({$set:{val:null}}), he deleted my entire document.

+4
source share
1 answer

Customization undefined, it worked like a charm!

db.properties.find(query).forEach(function(prop){
    db.properties.update(query, {$set:{ua: prop.val, val:undefined}}); 
});
+5
source

All Articles