Karan Kumar
Karan Kumar

Reputation: 3186

Node - Mongoose update method issue

I need to update and already queried document:

In memory save way works fine:

project.likers.push(profile.id);
project.set({
  likes: project.likes + 1
})
await project.save();

But I read in a blog that this way is slower as compared to using mongoose update method.

Since I am using multiple queries in this endpoint, I want to use mongoose update method for the same reason.

Doesn't work

project.update({ "$push": { "likers": profile.id }, "$inc": { "likes": 1 } });

Thanks in advance!

Upvotes: 1

Views: 41

Answers (1)

Yunus UYANIK
Yunus UYANIK

Reputation: 249

$push : The operator appends a specified value to an array.

And there isn't command 'update'. There are two command for update and they are :

  1. updateOne
  2. updateMany

If you just want to update the existing field, you can use like below:

db.collectionname.updateMany({ "likers": profile.id }, { "$inc": { "likes": 1 } })

If you want more info, check out.

Upvotes: 1

Related Questions