jeudi 22 avril 2021

How to represent polymorphic pattern when designing mongo db schema using a collection-relationship diagram

I'm designing a schema for mongo db to support app data modeling using a collection-relationship diagram that is widely used in official mongo db modeling documentation and tutorials. In some collections, I decided the best approach is to use Polymorphic Pattern.

As a simple fictional example, consider I want to embed profile information into a users collection. From the business domain, I know it exists two types of profiles: reader and administrator. Each profile shares some data fields, but also has different fields.

Applying the polymorphic pattern, a reader profile could be represented in a document like:

{
 _id: "<ObjectId>",
 email: "reader@email.com",
 ...
 profile: {
   type: "reader",
   name: "Reader's Name",
   total_reads: 1000,
   ...
 }
}

And an administrator profile could be represented in a document like:

{
 _id: "<ObjectId>",
 email: "administrator@email.com",
 ...
 profile: {
   type: "administrator",
   name: "Administrator's Name",
   total_edits: 100,
   ...
 }
}

As you can see, it implies in two differents profile subdocuments structure depended on type field.

How I represent this polymorphic pattern when designing a schema using a collection-relationship diagram?

Aucun commentaire:

Enregistrer un commentaire