Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update items in an array/list with mongo c# driver?

With the recent update to 10gen c# driver for mongodb I want to update my code so it use the strongly typed version.

My previous call was :

var update2 = new UpdateBuilder();
var index = album.Ratings.IndexOf(rating);
update2.Set("Ratings." + index + ".Number", number);
update2.Set("Rating", album.Rating);
_session.Db().GetCollection<Album>("Album")
    .Update(Query<Album>.Where(x => x.Id == objId), update2); //this line is working

The new call would be :

update.Set(x => x.Ratings[index].Number, number);
//update2.Set("Ratings." + index + ".Number", number); previous call

But I get this exception :

Unable to determine the serialization information for the expression: (Album x) => x.Ratings.get_Item(WebApp.Areas.API.Controllers.RatingController+<>c__DisplayClass5.index).Number.

Is there any way I can update an item inside a List?

like image 446
VinnyG Avatar asked Oct 09 '12 15:10

VinnyG


1 Answers

Interesting problem. This works when using a constant like below:

var update = Update<Album>.Set(x => x.Ratings[0].Number, 10);

However, this apparently breaks when you use a variable, like you have done with index. This is definitely a bug. I have created a Jira issue for it here: https://jira.mongodb.org/browse/CSHARP-598.

This is most likely due to us not partially evaluating the expression before processing it.

like image 188
Craig Wilson Avatar answered Oct 15 '22 00:10

Craig Wilson