In the video lecture for the polymorphic pattern: the slide and material discussed how to use the “country” field to determine the type of address.
Writing the “country” field last in the address sub-documents makes sense from a human readability point of view as this is where we would expect it when reading or writing an address by eye.
However there is a downside to positioning a ‘type discriminator’ field like this last. This becomes apparent when using a “reader” style decoder as described at this link: http://mongodb.github.io/mongo-java-driver/3.0/bson/readers-and-writers/
In code we want to encounter the ‘type discriminator’ field as early as possible when traversing through the fields. This allows the correct sub-document codec to be determined early.
My advice has been to put a discriminator field either first or second (after an id) or as the last ‘common’ field. For example in the address sub-document this could be after “city”. This would more closely resemble the position used in the ‘schema version’ pattern examples.
This design decision isn’t important when using a Document or dictionary based decoding approach but becomes relevant if using or migrating to the Reader style.
Suggest including a few words on this for future courses if you agree and would like students to consider this slightly subtle point when designing their schemas.
Thoughts?
Thanks!