So I have a playlist document which includes an item array. Now I want to add a vote array to the item, but I am not sure how to do this. Currently this is how I add a new item...
playlist_collection.update(
{"PlaylistName": playlistName},
{"$push": {items: newitem}},
function(error, playlist){
if( error ) callback(error);
});
I know there are a few more posts about this so if there is a good example duplicate is fine.
Here is the object in the db
{ "PlaylistName" : "MyFirstPlaylist",
"_id" : ObjectId("4fe0c6570fb2321b39000001"),
"comments" : [ ],
"created_at" : ISODate("2012-06-19T18:35:03.462Z"),
"items" : [
{ "SongName" : "Test", "Location" : "/tmp/40dd4f78466c3c7171f1eaf448e8f1d6" }
]
}
I ended up using this....
PlaylistProvider.prototype.addVoteToItem = function(, itemname, vote, callback) {
this.getCollection(function(error, collection) {
collection.findOne({'PlaylistName':playlistName}, function (err, playlist) {
if( err ) callback( err );
playlist.items.forEach(function (item) {
for (var prop in item) {
if (prop == 'SongName'){
if(item[prop] == itemname){
console.log("Found "+item[prop])
if( item.votes === undefined ) item.votes = [];
item.votes.push(vote)
collection.save(playlist)
callback(null, playlist)
break;
}
}
}
})
})
})
}