Thursday, 5 September 2013

MongoDB C# foreach BsonElement

MongoDB C# foreach BsonElement

I have a C# class that I instantiate and use to do CRUD operations in a
collection
public class Property
{
[BsonId]
public long id { get; set; }
[BsonElement("address_line")]
public string address_line { get; set; }
[BsonElement("city")]
public string city { get; set; }
[BsonElement("zip")]
public string zip { get; set; }
}
I want to be able to use a foreach loop and construct an update command
using $set without specifying each element, something like this:
var updateValues = new List<UpdateBuilder>();
foreach (var element in property.Elements)
updateValues.Add(Update.Set(element.Name, element.Value));
collection.Update(Query.EQ("_id", property.id),
Update.Combine(updateValues));
I this possible ?

1 comment: