27

I want to do something like this, but the this keyword doesn't seem to be set inside the update statement.

db.items.update({foo:…}, {$set: {bar: this.foo}}, false, true)

Do I have to use eval to accomplish this?

dstrelau
  • 353
  • 1
  • 4
  • 5
  • 2
    There is a feature request for this: https://jira.mongodb.org/browse/SERVER-458 – Thilo Sep 15 '11 at 07:31
  • 1
    Possible duplicate of [Update MongoDB field using value of another field](https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – Xavier Guihot Jun 11 '19 at 21:20

1 Answers1

5

Yes, you can't refer to other fields in the modifier. You'll have to use db.eval.

kris
  • 23,024
  • 10
  • 70
  • 79