Update with expression instead of value
mongodb
Solution
You can't use expressions in updates. Or, rather, you can't use expressions that depend on fields of the document. Simple self-containing math expressions are fine (e.g. `2 * 2`).
If you want to set a new field for all documents that is a function of other fields, you have to loop over them and update manually. Multi-update won't help here.
Problem
I am totally new to MongoDB... I am missing a "newbie" tag, so the experts would not have to see this question. I am trying to update all documents in a collection using an expression. The query I was expecting to solve this was: ``` db.QUESTIONS.update({}, { $set: { i_pp : i_up * 100 - i_down * 20 } }, false, true); ``` That, however, results in the following error message: ReferenceError: i_up is not defined (shell):1 At the same time, the database did not have any problem with eating this one: ``` db.QUESTIONS.update({}, { $set: { i_pp : 0 } }, false, true); ``` Do I have to do this one document at a time or something? That just seems excessively complicated. Update Thank you Sergio Tulentsev for telling me that it does not work. Now, I am really struggling with how to do this. I offer 500 Profit Points to the helpful soul, who can write this in a way that MongoDB understands. If you register on our forum I can add the Profit Points to your account there.