Update with AddToSet not updating null with MongoDB C #

Using MongoDB, I am unable to add the en element to the array when the array is null. AddToSet works as expected if I add an 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 the expected behavior? If so, is there a more general way to add elements to an array?

+5
source share
1 answer

I did some digging in accordance with some other comments - as you say, if an element does not exist, it works, but if it is null - that works. Apparently this is by design.

BsonIgnoreIfNull , , AddToSet .

+10

All Articles