J
J
JIakki2015-11-20 13:09:54
MongoDB
JIakki, 2015-11-20 13:09:54

How to change all mongodb value?

Is it possible to redo the change request more rationally?

User.findOne({_id: token.id}, function(err, user) {
    
    // для кожного елементу 
    for(var elem of user.data.categories) {

      // скинути баланс до нуля
      User.update({
        "_id" : token.id,
        'data.categories.name': elem.name
      }, {
        '$set': {
          'data.categories.$.balance': 0
        }
      }, function(){
        if(err) console.log(err)
      })

    }
  })

Since it is not possible to immediately update everything through User.update

Answer the question

In order to leave comments, you need to log in

2 answer(s)
⚡ Kotobotov ⚡, 2015-11-20
@angrySCV

use the "multi" key
https://docs.mongodb.org/manual/reference/method/d...

L
lega, 2015-11-20
@lega

You can read the document, reset everything and write it back (with a 2-phase commit or lock).
You can also revise the structure of the document, since you need such operations, for example, you can try to store categories not as an array, but as a dictionary:

data:{
  categoryName1:{ balance: 0},
  categoryName2:{ balance: 0}
}
Then it will be possible to update all categories at once by direct keys

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question