Data Modeling Flashcards
What are the 3 different types of data relationships?
Give examples
- One-to-one: User => Name
- One-to-many: User => Tasks
- Many-to-many => Products => Categories
What is the difference between normalized and denormalized data in MongoDB?
In the context of MongoDB, normalized data is data that is divided into different collections which normally improves write performance but decreases read performance. Normalized data does not support data duplication. Denormalized data is data that is combined from multiple collections into a single collection which normally improves query performance but decreases write performance. Denormalized data supports data duplication
What are the 2 ways to model data in MongoDB?
- Embedding documents (denormalizing)
- Referencing documents (normalizing)
What are the 3 steps of the schema design process?
- Identify the operations that your application runs most frequently
- Identify the relationships in your application’s data and decide whether to reference or embed related data
- Apply schema design patterns to optimize reads and writes
When might data duplication be beneficial?
Give an example
Given collection A and collection B, where A uses a subset of the data in B, and that data is not updated frequently, duplication might be beneficial. For example:
A product collection uses the 5 most recent documents from the review collection. In this case, instead of using references, code duplication could be beneficial since reviews are not updated frequently and keeping the data consistent between the collections is not a major task
Describe the behavior of the following code:
db.customers.insertOne( { customerId: 123, name: "Alexa Edwards", email: "a.edwards@randomEmail.com", phone: "202-555-0183" } ) db.products.insertOne( { productId: 456, product: "sweater", price: 30, size: "L", material: "silk", manufacturer: "Cool Clothes Co" } ) db.orders.insertOne( { orderId: 789, customerId: 123, totalPrice: 45, date: ISODate("2023-05-22"), lineItems: [ { productId: 456, product: "sweater", price: 30, size: "L" }, { productId: 809, product: "t-shirt", price: 10, size: "M" }, { productId: 910, product: "socks", price: 5, size: "S" } ] } )
The code displays a logical approach to duplicating product documents (with specific fields) inside order documents
Review
Review
db.articles.insertOne( { title: "My Favorite Vacation", date: ISODate("2023-06-02"), text: "We spent seven days in Italy...", tags: [ { name: "travel", url: "<blog-site>/tags/travel" }, { name: "adventure", url: "<blog-site>/tags/adventure" } ], comments: [ { name: "pedro123", text: "Great article!" } ], author: { name: "alice123", email: "alice@mycompany.com", avatar: "photo1.jpg" } } )
or
db.articles.insertOne( { title: "My Favorite Vacation", date: ISODate("2023-06-02"), text: "We spent seven days in Italy...", authorId: 987, tags: [ { name: "travel", url: "<blog-site>/tags/travel" }, { name: "adventure", url: "<blog-site>/tags/adventure" } ], comments: [ { name: "pedro345", text: "Great article!" } ] } )
What are MongoDB’s 5 main anti-pattern?
- Unbounded document size
- Massive number of collections
- Reading without indexes
- Redundant indexes
- Data that is accessed together is not stored together
What are the 2 tools that MongoDB Atlas provides to identify anti-patterns?
- Data explorer
- Performance Advisor (Tier M10)