SensioLabs

@sensiolabs   sensiolabs.com

MongoDB

Avancé

Sous titre

@sensiolabs   sensiolabs.com

Introduction to Schema Design

SensioLabs

@sensiolabs   sensiolabs.com

​Introduction
Concepts clés

Flexible schema

Application driven-schema

Rich document

Pre join / Embed document

No mongo join

No constraints

No transactions

Atomicity

SensioLabs

@sensiolabs   sensiolabs.com

One to one

relationship

users

{
    _id: "joe",
    name: "Joe Bookreader"
}

addresses

{
user_id: "joe",
    street: "123 Fake Street",
    city: "Faketon",
    state: "MA",
    zip: "12345"
}

@sensiolabs   sensiolabs.com

One to one

relationship

users
{
    _id: "joe",
    name: "Joe Bookreader",
    address: {
        street: "123 Fake Street",
        city: "Faketon",
        state: "MA"
    }
}

SensioLabs

Frequency of access

Document Growth

Atomicity

@sensiolabs   sensiolabs.com

One to many relationship

cities

{
    name: "Paris",
    region: "IDF",

    people: [

        ...
    ]
}

SensioLabs

Too many people...

@sensiolabs   sensiolabs.com

One to many relationship

SensioLabs

Duplication of data = inconsistency

people

{
    name: "Joe",

    city: {

        name: "Paris",
region: "IDF"

    }

}

people

{
    name: "Jane",

    city: {

        name: "Paris",
region: "IDF"

    }

}

@sensiolabs   sensiolabs.com

One to many relationship

SensioLabs

Make sure the city exist (no foreign key)

people

{
    name: "Joe",

    city: "Paris"

}

cities

{
    _id: "Paris",
    region: "IDF"

}

Multiple queries

@sensiolabs   sensiolabs.com

One to few

relationship

SensioLabs

No data inconsistency

people

{
    name: "Joe",

    addresses: [

        {...},
        {...},
{...}

    ]

}

@sensiolabs   sensiolabs.com

One to many / One to few

relationship

SensioLabs

What is the cardinality of the relationship ?

One to many => referencing

One to few => embedding

@sensiolabs   sensiolabs.com

SensioLabs

Use multikey indexes for performance

students

{
    _id: 0,

    name: 'joe',

    teachers: [2, 11, 41]

}

teachers

{
    _id: 11,

    name: 'John',

    students: [...]

}

Use reference

Many to many

relationship

@sensiolabs   sensiolabs.com

SensioLabs

Find the descendants and the ancestors of a node

Tree structure

Array of ancestors

ancestors: [ "Books", "Programming", "Databases" ]

ancestors: [ "Books", "Programming" ]

ancestors: [ "Books" ]

ancestors: [ ]

@sensiolabs   sensiolabs.com

SensioLabs

JSON schema validation since MongoDB 3.6

Schema validation

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major", "gpa" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            ...
         }
      }
   }
})
@sensiolabs   sensiolabs.com

SensioLabs

MongoDB 4.0 will support Multi-Document ACID Transactions

The futur of MongoDB

Sous titre

@sensiolabs   sensiolabs.com

Aggregation

SensioLabs

@sensiolabs   sensiolabs.com

Types d'aggregations

​Single Purpose Aggregation Operations

Aggregation Pipeline

Map-Reduce

SensioLabs

@sensiolabs   sensiolabs.com

​Single Purpose Aggregation Operation

count

distinct

SensioLabs

@sensiolabs   sensiolabs.com

Exemple

SensioLabs

@sensiolabs   sensiolabs.com

Aggregation Pipeline

Un Framework pour effectuer des analyses ou statistiques et générer des rapports pré-agrégés

SensioLabs

@sensiolabs   sensiolabs.com

Exemple

SensioLabs

@sensiolabs   sensiolabs.com

Map-Reduce

Map Reduce opération sous stéroïde

Limitation à 16MB d'output en BSON

SensioLabs

@sensiolabs   sensiolabs.com

Map-Reduce

Redondance et Disponibilité des données

@sensiolabs   sensiolabs.com

Replicat Sets

SensioLabs

@sensiolabs   sensiolabs.com

Réplication

SensioLabs

@sensiolabs   sensiolabs.com

Réplication

Un noeud Primary

Transparent pour le développeur

Toutes les opérations se font par défaut sur le Primary

SensioLabs

@sensiolabs   sensiolabs.com

Disponibilité

SensioLabs

@sensiolabs   sensiolabs.com

Disponibilité

Si un noeud Primaire ne réponds pas pendant 10s un autre noeud prendra sa place

 

Election - scrutin majoritaire - pour élire un nouveau noeud primaire

SensioLabs

@sensiolabs   sensiolabs.com

Disponibilité - Election

SensioLabs

@sensiolabs   sensiolabs.com

Disponibilité - Election

SensioLabs

@sensiolabs   sensiolabs.com

Exemple

Distribution des données sur plusieurs machines

@sensiolabs   sensiolabs.com

Sharding

SensioLabs

@sensiolabs   sensiolabs.com

Pourquoi ?

Vertical Scaling

Horizontal Scaling

  • Trop de données (1terra byte)
  • Trop d'opération (1million req/s)

SensioLabs

@sensiolabs   sensiolabs.com

Sharded Cluster

SensioLabs

@sensiolabs   sensiolabs.com

Shard Keys

- Une collection

- Un index simple ou composé

- Champ obligatoire / immutable

- Ne peux pas être changé

- Performance et scalability

SensioLabs

@sensiolabs   sensiolabs.com

Chunk

+ Sharded Cluster Balancer

SensioLabs

@sensiolabs   sensiolabs.com

Sharding Strategy

- Ranged Sharding

- Hashed Sharding

SensioLabs

@sensiolabs   sensiolabs.com

Ranged sharding

SensioLabs

@sensiolabs   sensiolabs.com

Ranged sharding

SensioLabs

@sensiolabs   sensiolabs.com

Hashed sharding

SensioLabs

@sensiolabs   sensiolabs.com

Tips

Mongodb calcule automatiquement le hash dans les requêtes

SensioLabs

@sensiolabs   sensiolabs.com

Ranged vs Hashed

SensioLabs

@sensiolabs   sensiolabs.com

Ranged vs Hashed

SensioLabs

@sensiolabs   sensiolabs.com

Exemple

sh.shardCollection("db.articles", {name: "hashed"})

Sous titre

@sensiolabs   sensiolabs.com

Sharding Conclusion

SensioLabs

@sensiolabs   sensiolabs.com

Avantages

- Storage Capacity

- Reads / Writes (Load balancing)

- High Availability

SensioLabs

@sensiolabs   sensiolabs.com

Inconvénients

- Filtrer les requêtes via la shared key

updateOne(), removeOne(), deleteOne()

 

- Tous les index doivent être préfixés par la shared key

@sensiolabs   sensiolabs.com

Thank you !

MongoDB Avancé

By skigun

MongoDB Avancé

  • 291