4 Pattern in MongoDB1
4 Pattern in MongoDB1
▪ Common Language
▪ Data Architects and Engineers
can easily reference the same
things
What are Patterns?
▪ Patterns are the most powerful tool for designing schemas
for MongoDB and NoSQL.
▪ Patterns are not full solution to problems. Patterns are a smaller
section of those solutions.
▪ Patterns are reusable units of knowledge.
▪ Familiar with software architecture design, patterns will do
for data modeling and schema design for document
databases.
What can patterns do for you?
▪ Improve Performance
▪ By using no more resources
than you should
▪ Simplify the access to the data
▪ By grouping and pre-arranging
data in a simpler form
Patterns in Schema Design - MongoDB
▪ Benefits of Patterns
▪ Pattern helps to optimize large documents with subset pattern.
▪ Use the computed pattern, avoid repeated calculations,
▪ Handle changes to the system implementation in no time.
▪ Patterns serve as a common language for teams working on schema
designs.
▪ Having clear patterns and understanding when and how to use them
eliminates errors in the data model for MongoDB and makes the
process more predictable.
Handling Duplication, Staleness and Integrity
▪ Example:
▪ We transpose the fields input,
output, and capacity.
Using the attribute pattern
▪ Example (cont.)
▪ For consistency, let's use K for key and V for value, as some of our
aggregation functions do.
▪ Under the field name K, we put the name of the original field as
the value
▪ For the first one, the field was named "input," so that became the
value for K.
▪ Then the value for input was five volts or 1,300 milliamps, so this is
the value for the field V
Using the attribute pattern
▪ Example (cont.):
Using the attribute pattern
▪ Example (cont.)
▪ Repeating the same thing for the original field's output and
capacity, we get three documents, each adding a K and a V in
them.
Using the attribute pattern
▪ Example (cont.)
▪ Because of their similar shape it is easy to place them together
under an "add_specs" for additional specs array.
▪ Note that for the third field, not only do I transpose it to a key value
pair, but that also added a third field called U to store some units
separately.
▪ This third field qualifies the relationship between K and the V.
Using the attribute pattern
▪ Example (cont.)
▪ The last thing to do is to create an index for all that info.
▪ This is done by creating an index on "add_specs.k" and
"add_specs.v."
Fields that share Common Characteristics
▪ Another scenario
Fields that share Common Characteristics
▪ Problem ▪ Solution
▪ The attribute pattern ▪ Break the field/value into a sub-
addresses the problem of document with:
having a lot of similar fields in ▪ fieldA: field
a document. ▪ fieldB: value
▪ Search across many fields at ▪ Example:
once ▪ {“color”:”Blue”, “size”: “large”}
▪ Fields present in only a subset ▪ {[{“k”:”color”, “v”: “Blue”},
of the documents have many
▪ {“k”:”size”, “v”: “large”}]}
similar fields.
Fields that share Common Characteristics
{ "events.prado" : 1 }
Lab: Apply the Attribute Pattern
▪ Task: To address this issue, you've decided to change the
schema to:
▪ Use a single index on all event dates.
▪ Transform the field that tracks the date when a piece was acquired,
date_acquisition, so that it is also indexed with the values above.
▪ To ensure the validator can verify your solution, use "k" and "v" as
field names if needed.
▪ To complete this lab:
▪ Modify the following schema to incorporate the above changes:
Lab: Apply the Attribute Pattern
▪ To complete this lab:
Modify the following
schema to incorporate the
above changes:
Lab: Apply the Attribute Pattern
▪ Save your new schema to a file named pattern_attribute.json.
▪ Validate your answer on Windows by running in the CMD
shell:
validate_m320 pattern_attribute --file pattern_attribute.json