DEV Community

Cover image for πŸ“š Tip and Trick: MongoDB Attribute Pattern πŸ’»
Danny Chan for MongoDB Builders

Posted on

πŸ“š Tip and Trick: MongoDB Attribute Pattern πŸ’»

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"
}
Enter fullscreen mode Exit fullscreen mode


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"),
}
Enter fullscreen mode Exit fullscreen mode

Index:

{release_Italy: 1}
{release_France: 1}
{release_UK: 1}
Enter fullscreen mode Exit fullscreen mode



After:

{
    releases: [
        {
        location: "USA",
        date: ISODate("1977-05-20T01:00:00+01:00")
        },
        {
        location: "France",
        date: ISODate("1977-10-19T01:00:00+01:00")
        }
    ],
}
Enter fullscreen mode Exit fullscreen mode

Index:

{ "releases.location": 1, "releases.date": 1}
Enter fullscreen mode Exit fullscreen mode

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" }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Index

{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Enter fullscreen mode Exit fullscreen mode



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" }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Index:

{"specs.k": 1, "specs.v": 1, "specs.u": 1}
Enter fullscreen mode Exit fullscreen mode



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

Image description

Danny Chan, specialty of FSI and Serverless

Image description

Kenny Chan, specialty of FSI and Machine Learning

Top comments (2)

Collapse
 
sc0v0ne profile image
sc0v0ne

Very good !!!

Collapse
 
danc profile image
Danny Chan

Happy learning