So today I realized I have a problem with updating/pushing to an array. I have an array in a class that is just a auto property
public List<things> Things { get; set; }
This can get put in the database as null and if I later on need to write a query like
var query = Query.EQ("_id", something.Id);
var update = Update.Push("Things", thing.ToBsonDocument());
coll.Update(query, update);
I now have a problem because my update will throw an exception that I tried to push to a NULL array.
I solved this by just putting a private backer on the class
private List<things> _things = new List<things>();
public List<things> Things { get { return _things;} set { _things = value;} }
Now at least a new instance will have an empty array and someone would need to explicitly say
Things = null
But I was wondering is there a better way to solve this. Decorators? Mongo Index? something to say hey this field must ALWAYS be an array.
I've always been kind of dubious of auto properties and prefer using the private backer especially for non primitives. Just wondering if a more robust solutions exist that's built into the Mongo engine.