我在MongoDB中有这个文档结构。 我需要为给定的艺术家,专辑ID找到并更新一首歌 当有人喜欢一首歌时,我需要增加喜欢的数量并将用户添加到投票列表中。
示例文件:
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"}]
}]
}]
}]
这就是我所拥有的,但它不起作用。
//注意艺术家ID,专辑ID和歌曲ID正确传递
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