MongoDB Data Modeling

Han Yi

Know your business

Determine your position

2.Data Model

Logical Model

Physical Model

1.Domain Model

?

Master your kits

vs

Core concept: Data as a Document

Why Document ?

  • Humongous support
    • DoubleClick, 2007
    • 400000 ads/s
  • Flexible types for data model
    • Key-Value
    • List
    • Rich document
    • Embedded array and document
    • ...

Pros vs Cons of document model

  • Pros
    • ​Flexible data model design
    • High performance read
  • Cons
    • Design heavily depends on scenario
    • Single document should be no larger than 16mb, no deeper than 100 layers

Data model patterns for a document

  • Normalized
    • referenced in different document
  • Non-normalized
    • nested in single document
  • Tree structure
    • hybrid pattern

MongoDB prefer

  • Non-normalized

Real case

  • Fan Out Read
  • Fan Out Write

3 Factors of data modeling

  • Performance
  • Query pattern
  • Life cycle of data and growth rate of documents (Application domain)

Sample 1: Date modeling

  • BSON Date type is 64 bits signed number
  • BSON Date type is UTC time
  • Time zone is suggested using additional fields:

var now = new Date();

db.data.save( { date: now,

                            offset: now.getTimezoneOffset() } );

Sample 2: Monetary data modeling

  • Numeric Model
    • Decimal format (3.4+)
    • IEEE 754-2008: Decimal 128
    • Type long (64 bits) + Power factor (older than 3.4)
  • Non-numeric Model
    • Full value: String
    • Approximate value: double

Sample 3: Content query modeling

  • $regex
  • $text
  • Keywords array: multikey index

http://mongoing.com/

Made with Slides.com