1

I have this document structure in MongoDB. I need to find and update a song for a given artist, album id When someone likes a song, I need to increase the likes count and add the user to the votes list.

Sample document:

artists = [{
    "_id": 1,
    "name": "Bob",
    "albums": [{
        "_id": 3,
        "numSongs" : 2,
        "songs": [{
                    "_id": 4,
                    "title": "Song 1",
                    "numLikes": 2,
                    "votes" : [{"usr":"John"}, {"usr": "Steve"}]
                  },
                  {
                    "_id": 5,
                    "title": "Song 2",
                    "numLikes": 3,
                    "votes" : [{"usr": "Mark"}, {"usr": "Ken"}, {"usr": "Luke"}]
                  }]
         }]
     }]

This is what I have but it is not working.

//Note artists id, album id and song id are passed correctly

var query = Query.And( Query.EQ("_id", artistId), Query.EQ("albums._id", albumId), Query.EQ("albums.$.songs._id", songId));

var update = Update.Push("albums.$.songs.votes", newVote.ToBsonDocument()).Inc("albums.$.songs.numLikes", 1);

WriteConcernResult result = Artists.Update(query, update);

numDocsUpdated = result.DocumentsAffected;//This is always 0
kheya
  • 7,546
  • 20
  • 77
  • 109

0 Answers0