Mongodb Increment value inside nested array

后端 未结 1 1363
南旧
南旧 2020-11-30 12:19

I´m using mongotemplate for Spring, and I was wondering how I could increment a value of one of my documents that I have in an array atomically. Imagine that we have

相关标签:
1条回答
  • 2020-11-30 13:01

    To update an element in an array field, you can use the positional $ operator

    For example, the following js increments c from 3 to 4 for _id 341445:

    db.collection.update({a:1, "b._id":341445} , {$inc:{"b.$.c":1}})
    
    0 讨论(0)
提交回复
热议问题