Due to non schema nature of mongodb collection, one collection may contains distinct number of fields.Its not recommended though, its possible to contain different number of fields due to business requirements. So for example say we have a collection “Client”, under which there is 2 document. Second document lacks 1 field “City”. So while binding back to POCO via c# driver , we get exception “Element does not match any field or property of class”.
When a BSON document is deserialized back to POCO, the name of each element is used to look up a matching field or property in the class map, when deserializer do not find the mapping property its throws an exception, that we have in this scenario. If we want to ignore those extra properties, there is two possible ways, during deserialization or during initialization of the BsonClassMap. I prefer the while I do the mapping, I live to keep my POCO clean.
As you can see the above code block, SetIgnoreExtraElements methods is used to specify inside mapping to ignore extra elements from BSON document.
That’s it, now you can work with your POCO more easily.