Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$inc with mongoose ignored

I am using the following code to add some content in an array and increment two different counters.

The item is properly pushed in the array, and the pendingSize is properly incremented. But the unRead is never incremented. It used to increment, then today it stopped. The value of the unRead field in my mongodb collection ( hosted on mongohq ) is set to 0 (numerical, not string)

When I look in my console, I see 'update success'.

any clue why it stopped working ?

Thanks

Notifications.update({ "id" : theid}, { $push: { pending: notification}, $inc: { unRead : 1 }, $inc: { pendingSize: 1 }}, function(err){
                    if(err){
                        console.log('update failed');
                        callback("Error in pushing." + result.members[i]);
                    }
                    else{ 
                        console.log('update succes');
                        callback("Success");
                    }
                });
like image 481
guiomie Avatar asked Dec 17 '22 03:12

guiomie


1 Answers

Combine the args of $inc into a single nested object, like this:

$inc: { unRead : 1, pendingSize: 1 }

Objects represented in JSON are key:value, where the keys must be unique, so trying to specify multiple values for $inc won't work.

like image 167
mpobrien Avatar answered Dec 20 '22 16:12

mpobrien