Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS MongoDB Update $inc

I'm not succeeding in updating my mongodb collection with the $inc operator. I've created a test collection to simplify things.

My code:

collection.update({field1: 'text'}, { $inc: { items: 5 } }, function (error, result) {
    if (error) reject(error);
    else resolve(result);
});

My collection:

/* 1 */
{
    "_id" : ObjectId("55d2d56a931d867cfeeba4cb"),
    "field1" : "text",
    "items" : 0
}

The update operation returns no errors, response is OK with 0 updated documents.

What am I doing wrong?

P.S. My mongodb library version is 2.0.39

[edit] I've also tried updateOne method since I saw update is deprecated in version 2, same result though

like image 431
AlexD Avatar asked Jan 27 '26 17:01

AlexD


1 Answers

Ended up realizing had unrelated connection to mongo issues, the above code actually works very well.

like image 55
AlexD Avatar answered Jan 30 '26 11:01

AlexD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!