Tuesday, May 21, 2024

MongoDB Patterns

Certainly! Here's a concise cheat sheet covering various MongoDB data modeling patterns with schema design and a retail domain example for each:


Embedded Data Pattern

  • Description: Store related data within a single document using nested structures.
  • Schema Design:
    json
  • { "_id": ObjectId("..."), "order_id": "ORD123", "customer": { "name": "John Doe", "email": "john@example.com", "address": { "street": "123 Main St", "city": "Anytown", "country": "USA" } }, "products": [ { "name": "Product 1", "quantity": 2, "price": 50 }, { "name": "Product 2", "quantity": 1, "price": 75 } ] }
  • Retail Domain Example: Order document containing customer details and ordered products.

Normalized Data Pattern

  • Description: Organize related data across multiple collections and establish relationships using references.
  • Schema Design:
    json
  • // Customers collection { "_id": ObjectId("..."), "name": "John Doe", "email": "john@example.com" } // Orders collection { "_id": ObjectId("..."), "customer_id": ObjectId("..."), "order_id": "ORD123", // Other order fields... } // Products collection { "_id": ObjectId("..."), "name": "Product 1", "price": 50 // Other product fields... }
  • Retail Domain Example: Separate collections for customers, orders, and products with references between them.

Array of Objects Pattern

  • Description: Store related data as an array of objects within a document.
  • Schema Design:
    json
  • { "_id": ObjectId("..."), "customer": "John Doe", "orders": [ { "order_id": "ORD123", "products": [ { "name": "Product 1", "quantity": 2, "price": 50 }, { "name": "Product 2", "quantity": 1, "price": 75 } ] } ] }
  • Retail Domain Example: Customer document with an array of orders, each containing ordered products.

Bucketing Pattern

  • Description: Group related data into buckets or categories within a single collection.
  • Schema Design:
    json
  • { "_id": ObjectId("..."), "timestamp": ISODate("..."), "category": "sales", "order_id": "ORD123", // Other sales-related fields... }
  • Retail Domain Example: Sales data bucketed by categories like orders, returns, discounts, etc.

Polymorphic Pattern

  • Description: Accommodate different types of data within a single collection.
  • Schema Design:
    json
  • { "_id": ObjectId("..."), "entity_type": "customer", // Customer fields... } { "_id": ObjectId("..."), "entity_type": "product", // Product fields... } { "_id": ObjectId("..."), "entity_type": "order", // Order fields... }
  • Retail Domain Example: Documents representing customers, products, and orders stored in a single collection.

Shredding Pattern

  • Description: Decompose complex, nested structures into simpler, flatter documents.
  • Schema Design:
    • Decompose nested structures into separate collections and establish relationships using references.
  • Retail Domain Example: Decompose order documents into separate collections for customers, orders, and products.

Document Versioning Pattern

  • Description: Track changes to documents over time.
  • Schema Design:
    json
  • { "_id": ObjectId("..."), "order_id": "ORD123", "status": "shipped", "__v": 1 // Version number }
  • Retail Domain Example: Order documents with a versioning field to track status changes.

By utilizing these patterns with appropriate schema designs in a retail domain context, you can effectively model your data in MongoDB to handle various aspects of a retail business, such as orders, customers, products, and sales data.


No comments: