AlexD
AlexD

Reputation: 4290

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

Upvotes: 3

Views: 1197

Answers (2)

Shekhar Tyagi
Shekhar Tyagi

Reputation: 1674

I am using this , as similar in my code.Hope this helps.

 collection.update({field1:'text'}, {$inc: {items: 5}}, function (err, product) {
                if (err) {
                    res.json({error: 1, message: err, data: []});
                } else {
                    res.json({error: 0, message: 'success', data: {count_likes: product}});
                }
            });

Upvotes: 0

AlexD
AlexD

Reputation: 4290

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

Upvotes: 1

Related Questions