How do you update a field value in Mongo to match a different field value in the same document?
I have several documents:
{"required" : 100, "total" : 30}
and I want to update the docs in such a way that require = total (regardless of the value of total). I tried:
db.collection.update({}, {"$set" : {"required" : "total"}})
but this sets it to the string literal "total", how do I access the value of the field, in this case 30
.
+3
nickponline
source
to share
1 answer
You cannot do this. Try this instead:
db.collection.find().forEach(function(d) {
d.required = d.total;
db.collection.save(d);
});
+1
Trang tung nguyen
source
to share