What about arrays? Do I have to do this with each entry of the array
before insertion? And that's the only way?
That's insanely uncomfortable, I store Dictionary/Person/Enumerable/
Simple Type (like int/string/bool) - each one will need different
handling in order to put into the BsonDocument object.
I don't think I want to do all this work... what will be the
disadvantages of changing this from BsonDocument/BsonExtraElements to
a plain Dictionary<string, object>? What would stop working? Would
attributes still work on the dictionary values? Like if I put a Person
object in the dictionary and the Age member has [BsonDefaultValue],
would it still work?
Thank you
On Jan 4, 5:52 pm, Robert Stam <
rob...@10gen.com> wrote:
> [BsonExtraElements] is mostly intended to hold extra elements so that
> they successfully round trip from the database to your application and
> back. You can definitely do anything you want with them, but the values
> stored in them are of type BsonValue (some of which may be BsonDocuments),
> so you have to convert back and forth from instances of your class to
> BsonValue.
>
> You can't do this with simple casting. you have to serialize/deserialize
> your classes to/from BsonValue.
>
> To serialize an instance of Person as a BsonDocument and add it to your doc
> variable you can write this:
>
> var person = new Person();
> doc.Add("key1", person.ToBsonDocument());
>
> To go in the other direction you can write this:
>
> person = BsonSerializer.Deserialize<Person>(doc["key1"].AsBsonDocument);
>