MongoDB Schema Design Patterns – Polymorphic
Tags: mongodb, uncategorized, polymorphicpattern, schemadesign
Published: 2019-02-10 19:16:42

---
title: MongoDB Schema Design Patterns – Polymorphic
published: true
tags: MongoDB,Uncategorized,Polymorphic Pattern,schema design
canonical_url: https://www.kenwalger.com/blog/uncategorized/mongodb-schema-design-patterns-polymorphic/
---

One frequently asked question when it comes to MongoDB is “How do I structure my schema in MongoDB for my application?” The honest answer is, it depends. Does your application do more reads than writes? What data needs to be together when read from the database? What performance considerations are there? How large are the documents? How large will they get? How do you anticipate your data will grow and scale?

All of these questions, and more, factor into how one designs a database schema in MongoDB. It has been said that MongoDB is schemaless. In fact, schema design is very important in MongoDB. The hard fact is that most performance issues we’ve found trace back to poor schema design.

Over the course of this blog post series, we’ll take a look at twelve common Schema Design Patterns that work well in MongoDB. We hope this series will establish a common methodology and vocabulary you can use when designing schemas. Leveraging these patterns allows for the use of “building blocks” in schema planning, resulting in more methodology being used than art.

MongoDB uses a [document data model](https://docs.mongodb.com/manual/core/data-model-design/). This model is inherently flexible, allowing for data models to support your application needs. The flexibility also can lead to schemas being more complex than they should. When thinking of schema design, we should be thinking of performance, scalability, and simplicity.

Let’s start our exploration into schema design with a look at what can be thought as the base for all patterns, the _Polymorphic Pattern_. This pattern is utilized when we have documents that have more similarities than differences. It’s also a good fit for when we want to keep documents in a single collection.

#### The Polymorphic Pattern

When all documents in a collection are of similar, but not identical, structure, we call this the Polymorphic Pattern. As mentioned, the Polymorphic Pattern is useful when we want to access (query) information from a single collection. Grouping documents together based on the queries we want to run (instead of separating the object across tables or collections) helps improve performance.

Imagine that our application tracks professional sports athletes across all different sports.

We still want to be able to access all of the athletes in our application, but the attributes of each athlete are very different. This is where the Polymorphic Pattern shines. In the example below, we store data for athletes from two different sports in the same collection. The data stored about each athlete does not need to be the same even though the documents are in the same collection.

![Polymorphic Design Pattern with Common Fields](https://i1.wp.com/webassets.mongodb.com/_com_assets/cms/Polymorphic1-nanehssyv3.png?w=840&ssl=1)

Professional athlete records have some similarities, but also some differences. With the Polymorphic Pattern, we are easily able to accommodate these differences. If we were not using the Polymorphic Pattern, we might have a collection for Bowling Athletes and a collection for Tennis Athletes. When we wanted to query on all athletes, we would need to do a time-consuming and potentially complex join. Instead, since we are using the Polymorphic Pattern, all of our data is stored in one Athletes collection and querying for all athletes can be accomplished with a simple query.

This design pattern can flow into embedded sub-documents as well. In the above example, Martina Navratilova didn’t just compete as a single player, so we might want to structure her record as follows:

![Polymorphic Design Pattern with sub-documents](https://i0.wp.com/webassets.mongodb.com/_com_assets/cms/Polymorphic2-bpk0w3ig5y.gif?w=840&ssl=1)

From an application development standpoint, when using the Polymorphic Pattern we’re going to look at specific fields in the document or sub-document to be able to track differences. We’d know, for example, that a tennis player athlete might be involved with different events, while a different sports player may not be. This will, typically, require different code paths in the application code based on the information in a given document. Or, perhaps, different classes or subclasses are written to handle the differences between tennis, bowling, soccer, and rugby players.

#### Sample Use Case

One example use case of the Polymorphic Pattern is [Single View applications](https://www.mongodb.com/use-cases/single-view). Imagine working for a company that, over the course of time, acquires other companies with their technology and data patterns. For example, each company has many databases, each modeling “insurances with their customers” in a different way. Then you buy those companies and want to integrate all of those systems into one. Merging these different systems into a unified SQL schema is costly and time-consuming.

[MetLife](https://www.metlife.com/) was able to leverage MongoDB and the Polymorphic Pattern to build their single view application in a few months. Their Single View application aggregates data from multiple sources into a central repository allowing customer service, insurance agents, billing, and other departments to get a 360° picture of a customer. This has allowed them to provide better customer service at a reduced cost to the company. Further, using MongoDB’s flexible data model and the Polymorphic Pattern, the development team was able to innovate quickly to bring their product online.

A Single View application is one use case of the Polymorphic Pattern. It also works well for things like product catalogs where a bicycle has different attributes than a fishing rod. Our athlete example could easily be expanded into a more full-fledged content management system and utilize the Polymorphic Pattern there.

#### Conclusion

The Polymorphic Pattern is used when documents have more similarities than they have differences. Typical use cases for this type of schema design would be:

- Single View applications
- Content management
- Mobile applications
- A product catalog

The Polymorphic Pattern provides an easy-to-implement design that allows for querying across a single collection and is a starting point for many of the design patterns we’ll be exploring in upcoming posts. The next pattern we’ll discuss is the Attribute Pattern.

If you have questions, please leave a comment below.

* * *

_This post was originally published on the [MongoDB Blog](_wp_link_placeholder)._

[![Facebook](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png?resize=48%2C48&ssl=1 "Share on Facebook")](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F&t=MongoDB%20Schema%20Design%20Patterns%20%E2%80%93%20Polymorphic&s=100&p%5Burl%5D=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F&p%5Bimages%5D%5B0%5D=https%3A%2F%2Fwebassets.mongodb.com%2F_com_assets%2Fcms%2FPolymorphic1-nanehssyv3.png&p%5Btitle%5D=MongoDB%20Schema%20Design%20Patterns%20%E2%80%93%20Polymorphic "Share on Facebook")[![twitter](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png?resize=48%2C48&ssl=1 "Share on Twitter")](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F&text=Hey%20check%20this%20out "Share on Twitter")[![google_plus](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png?resize=48%2C48&ssl=1 "Share on Google+")](https://plus.google.com/share?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F "Share on Google+")[![reddit](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png?resize=48%2C48&ssl=1 "Share on Reddit")](http://www.reddit.com/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F&title=MongoDB%20Schema%20Design%20Patterns%20%E2%80%93%20Polymorphic "Share on Reddit")[![linkedin](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png?resize=48%2C48&ssl=1 "Share on Linkedin")](http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F&title=MongoDB%20Schema%20Design%20Patterns%20%E2%80%93%20Polymorphic "Share on Linkedin")[![mail](https://i0.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png?resize=48%2C48&ssl=1 "Share by email")](mailto:?subject=MongoDB%20Schema%20Design%20Patterns%20%E2%80%93%20Polymorphic&body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fmongodb-schema-design-patterns-polymorphic%2F "Share by email")

The post [MongoDB Schema Design Patterns – Polymorphic](https://www.kenwalger.com/blog/uncategorized/mongodb-schema-design-patterns-polymorphic/) appeared first on [Blog of Ken W. Alger](https://www.kenwalger.com/blog).