Update a value in MongoDB based on its current val

2019-01-09 09:22发布

问题:

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?

回答1:

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



回答2:

Good example can be found here MongoDB: Updating documents using data from the same document



标签: mongodb