Say I have an object with field state
, I want to update this field, while keeping the previous value of state
in previous_state
field. First, I have tried to make an update with unset-rename-set:
collection.update(query, {$unset: {previous_state: ""}, $rename: {state: "previous_state"}, $set: {state: value}})
no surprise it did not work. After reading:
- Update MongoDB field using value of another field
- MongoDB update: Generate new field based on existing field, or update in place
- Update field with another field's value in the document
I am nearly convinced that I do not have a solution to perform this in a single query. So the question is what is the best practice to do it?