Answer the question
In order to leave comments, you need to log in
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)
})
}
})
Answer the question
In order to leave comments, you need to log in
use the "multi" key
https://docs.mongodb.org/manual/reference/method/d...
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 questionAsk a Question
731 491 924 answers to any question