In SQL you can do something like:
UPDATE test SET a = b + c
I am wondering if there is something I can do that is similar in MongoDB. For example something like:
db.test.update({},{$set: {a: b + c}})
However, when I try that it says b and c are not defined (which makes sense). Is there some way I can tell mongo to use the value of fields of the object it is updating? Or do I always need to do it in two steps–first retrieving the fields and then using them to construct the appropriate update statement.
You cannot do that with MongoDB in an update operation. You would need to use something like a map/reduce or db.eval to accomplish it and even then you’re still going to be technically running two queries, one to find and one to update.