Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose dot notation $inc update

I have a document as follows:

{
  "_id" : "1",

  "myplaylists" : [{
      "_id" : "2",
      "name" : "Playlist_0",
      "likes" : 712,
      "views" : 7110
    }],
  "name" : "First name, Last name"
}

and I want to do an update and increment "myplaylists.likes"

what I did is:

User.update( {'myplaylists._id': 2}, {$inc: { 'myplaylists.likes': 1 }}

but it doesn't seem to work because of the dot notation in the $inc

Any ideas how to do it?

like image 478
AboulEinein Avatar asked Jul 16 '12 23:07

AboulEinein


1 Answers

Use the positional operator $ to identify the matched array element to update:

User.update( {'myplaylists._id': 2}, {$inc: { 'myplaylists.$.likes': 1 }}
like image 118
JohnnyHK Avatar answered Oct 23 '22 06:10

JohnnyHK