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
Top comments (2)
Very good !!!
Happy learning