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
}
And after the update it turns to:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : 9876541,
"ua" : 9876541
}
But I expect it as:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : null,
"ua" : 9876541
}
But its not working. also setting "val"
null ({$set:{val:null}})
directly it deleted my entire document.
Indeed, it's not possible to store null values in a MongoDB document using a DataFrame. The Python None values are considered as missing attributes accordingly to this NoSQL specific allowance. However, if your column is numerical, you can force writing a null value by setting it to NaN.
We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value.
You cannot update it but you can save a new id and remove the old id.
Solution 1: In case preservation of all null values[] or null fields in the array itself is not necessary. Filter out the not null elements using $filter , the ( all null elements) array would be empty, filter that out from documents using $match then $sort on values .
Setting undefined
, It worked like a charm!!
db.properties.find(query).forEach(function(prop){
db.properties.update(query, {$set:{ua: prop.val, val:undefined}});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With