Situation:
- Single View applications 🔍
- Incorporate any type of data 🌐
- Dynamic schemas to iterate 🔀
- Expressive query language, indexing, aggregation to find, filter the data 🔍
Challenge:
- Diverse Data Types: Reconciling schemas from different systems is hard 🤯
- Rigid Schemas: Cannot change on schema 📛
Shortcomings:
- Only can fine-grained data schema 📋
- Cannot add ad hoc queries 🚫
- Cannot add secondary indexes 🚫
Solution: MongoDB Documents model 💥
- Use any type of data (numbers, strings, binary data, arrays) 🔢🔤🔣🔢
- Dynamic Schemas: Don't need to have all the same fields 🔀
- Query example: Indexing to find and filter data 🔍
- Indexing: pet
{
"name": "Peter"
}
{
"name": "Mary",
"pet": "cat"
}
Result:
- Only can query Mary because she has pet on indexing 😕
Attribute Pattern: 🔑
- Subset: Big documents with many similar fields 📜
- Moving subset of data into a key-value sub-document 📂
- Sort fields in a small subset of documents 🗂️
Advantage:
- Avoid create a lot of indexes and reduce performance 💪
Attribute Pattern Use Case 1: Insurance Company 🏦
- Aggregates data from multiple sources into a central place 🌐
- Get a 360° picture of a customer 🔍
- Better customer service at a reduced cost 💰
- Innovate quickly 🚀
Before:
{
release_Italy: ISODate("1977-10-20T01:00:00+01:00"),
release_France: ISODate("1977-10-19T01:00:00+01:00"),
release_UK: ISODate("1977-12-27T01:00:00+01:00"),
}
Index:
{release_Italy: 1}
{release_France: 1}
{release_UK: 1}
After:
{
releases: [
{
location: "USA",
date: ISODate("1977-05-20T01:00:00+01:00")
},
{
location: "France",
date: ISODate("1977-10-19T01:00:00+01:00")
}
],
}
Index:
{ "releases.location": 1, "releases.date": 1}
Non-Deterministic Naming: 🔖
- Easy to add extra qualifiers 🔍
- Clearly state relationship of original field and value 📝
{
"specs": [
{ k: "volume", v: "500", u: "ml" },
{ k: "volume", v: "12", u: "ounces" }
]
}
Index
{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Attribute Pattern Use Case 2: Clothing 👕
- Sizes that are expressed in small, medium, large 📏
- Some clothing in collection may be expressed in numbers (11, 12, 13) 🔢
- Characteristics are seldom common across the assets 🎨
Catalog are similar, such as name, vendor, manufacturer, country of origin 🏭
- Provides good structure for data 🗂️
{
"specs": [
{ k: "size", v: "11", u: "CHINA" },
{ k: "size", v: "large", u: "USA" }
]
}
Index:
{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Reference:
https://www.mongodb.com/developer/products/mongodb/attribute-pattern/
Building with Patterns: The Attribute Pattern
https://www.mongodb.com/solutions/use-cases/single-view
Single View applications
https://www.mongodb.com/blog/post/building-with-patterns-a-summary
Building with Patterns: A Summary
https://www.mongodb.com/blog/post/building-with-patterns-the-approximation-pattern
Building with Patterns: The Approximation Pattern
Editor
Danny Chan, specialty of FSI and Serverless
Kenny Chan, specialty of FSI and Machine Learning

