I'm trying to use findAndModify operation to find and update doc.
Query: {'socket_id': data.socket_id, 'weapons.type': selectedWeapon, 'battleWeapons.type':selectedWeapon}
Update: {'$inc':{'weapons.$.nums':-1, 'battleWeapons.$.nums':-1}}
I got Doc in my callback function (so it found by query), but there's no changes in MongoDB doc. Same command works fine in mongo console. It changes everything I need. What am I doing wrong?
So the whole command looks like:
db.collection('users', function (err, collection) {
var condition = {'socket_id': data.socket_id, 'weapons.type': selectedWeapon, 'battleWeapons.type':selectedWeapon},
update = {'$inc':{'weapons.$.nums':-1, 'battleWeapons.$.nums':-1}};
collection.findAndModify(condition, [['_id','asc']], update, {'new': true}, function (err, item) {...
})});
In findAndModify, there is one more parameter which you can set, to return the old document(before update) or new document ( after update ). I am guessing this must be the issue. For confirmation, for the existing findAndModify() command, after this command is run, check that updated document from the mongo console, it must be changed.