Using MongoDB, I'm having trouble adding en element to an Array when the array is null. AddToSet works as expected if I add the item from the console. I am using the official C# driver from 10gen.
var query = Query.EQ("_id", objectId);
var itemDoc = item.ToBsonDocument();
//items is an array but currently null
var update = MongoDB.Driver.Builders.Update.AddToSet("items", itemDoc); // YUNoWork?
//somefield doesn't exist
var workingUpdate = MongoDB.Driver.Builders.Update.AddToSet("somefield", itemDoc); //works fine
var collection = DataBase.GetCollection<MyObject>(CollectionName);
collection.Update(query, update); // doesn't work
collection.Update(query, workingUpdate); // works
Is this expected behavior? If so, is there a more general way to add items to an array?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With