guiomie
guiomie

Reputation: 5148

$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");
                    }
                });

Upvotes: 4

Views: 4916

Answers (1)

mpobrien
mpobrien

Reputation: 4962

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.

Upvotes: 12

Related Questions