It seems that there are a few ways to serialise/ deserialise MongoDB data to/from objects
var docs = _collection.FindAs<MyType>(_document);
OR
BsonSerializer.Deserialize<MyType>(doc);
OR
var myClass = new Mytype();
myClass.Name = bsonDoc["name"].AsString;
I am wondering how to "use" the data in strongly types objects. But preserve the full Bsondocument - is it even possible? It seems that if I serialise into a strongly typed object and then saved it back - we would lose any information that wasn't in those objects.
In general, it is easiest to work with strongly-typed documents that map to your BSON structure in the collection. If not changed, the driver will complain if you load a document from the collection that has additional properties that cannot be mapped. This safe-guards against wreaking havoc in unexpected schemas.
You can change this behavior in several ways:
[BsonIgnoreExtraElements]. This bears the risk that you might loose data if you do a full replace on the document. In this case working with fine-grained updates as opposed to full-document-replacements is key in order to preserve the data that the application does not know about.Dictionary<string, object>to your collection and mark it with\[BsonExtraElements\]. This dictionary will hold all the unmapped properties. When the document is serialized again, the order may change.If your application is the only one that works with the data, option 1 works well and also configures the application to be tolerant against minor changes in the documents (e.g. adding a field).