Vous êtes sur la page 1sur 246

MongoDB

la base NoSQL qui rinvente la gestion de donnes

11/28/13

@dwursteisen

!1

MongoDB
Big Database
@DWURSTEISEN

http://fr.slideshare.net/soatexpert

WARNING

Il ny a pas si longtemps que cela,


un site internet faisait sensation

20:00:00

Ce site dboite

Oups ! Database Error

no more space disk available

G
I
B

A
T
A
D

t
a
o
S
n
o
i
t
a
m
i
An
3
1
0
2
e
c
n
a
r
F
x
x
o
v
De

Caractristiques
(sous le capot)

Orient document

{!

, !
"
1
e
m
g
i
n
e
"
, !
"
i
d
"_id":
e
r
d
n
e
v
e du
m
g
i
n
E
"
:
"
e
"titr
ue!
r
t
:
"
n
o
i
t
a
"activ

{!

, !
"
1
e
m
g
i
n
e
"
, !
"
i
d
"_id":
e
r
d
n
e
v
e du
m
g
i
n
E
"
:
"
e
"titr
ue!
r
t
:
"
n
o
i
t
a
"activ

{!

}!

"_id": "enig
me1", !
"activation"
: true, !
"joueurs": [
!
{ "email": "
john@doe.com
], !
", " s c o r e ":
20 }!
"indices": [
!
{!
"contenu": "
je suis ton
p r e ", !
" f l a s h c o d e ":
"111-111-111
1 ", !
"joueurs": [
{ "email": "
}
john@doe.com
]!
" } ]!

Sans schma

CREATE TABLE example_default_now (


id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
data VARCHAR(100),
created TIMESTAMP DEFAULT NOW()
);

CREATE TABLE example_default_now (


id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
data VARCHAR(100),
created TIMESTAMP DEFAULT NOW()
);

Sans transaction

Transaction

Transaction

Transaction

Transaction

Transaction

Transaction

Sans transaction

Sans transaction

Sans transaction

Sans transaction

Update atomique

Sans jointure

{!

}!

"_id": "enig
me1", !
"activation"
: true, !
"joueurs": [
!
{ "email": "
john@doe.com
], !
", " s c o r e ":
20 }!
"indices": [
!
{!
"contenu": "
je suis ton
p r e ", !
" f l a s h c o d e ":
"111-111-111
1 ", !
"joueurs": [
{ "email": "
}
john@doe.com
]!
" } ]!

{!

}!

"_id": "enig
me1", !
"activation"
: true, !
"joueurs": [
!
{ "email": "
john@doe.com
], !
", " s c o r e ":
20 }!
"indices": [
!
{!
"contenu": "
je suis ton
p r e ", !
" f l a s h c o d e ":
"111-111-111
1 ", !
"joueurs": [
{ "email": "
}
john@doe.com
]!
" } ]!

jointure ?

Distribu

Document = 16Mb Hard Limit

Design
des structures
des donnes

Type

Exemple

Int / Double /

{ a: 1 }

Boolean

{ b: true }

String

{ c: hello }

Array

{ d: [1, 2, 3] }

Date

{e: ISODate("2012-12-19T06:01:17.171Z")

ObjectId

{f: ObjectId(123456)}

Object

{g: {a: 1, b: true}}

http://docs.mongodb.org/manual/reference/bson-types/

Organiser
les donnes selon leurs

Utilisations

ci gt la
4me forme
normale

Question :

Structure de donne pour


laffichage dun message ?

Auteur

Auteur

Contenu

Auteur

Contenu

Recouicoui

{
_id: ObjectId(11),

{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,

{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: [ObjectId(22), ObjectId(33)]

_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: 2, // compteur

_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: [{user: {}, {user: {}]

_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: [{user: {}, {user: {}]

_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: [{user: {} * 789013]

_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: [{user: {} * 789013]

> 16Mb

Information partielle

{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,

recouicoui: {
users: [{user: {} * 5],
compteur: 789013
}

Le design dune

bonne
complexe

structure de donne est

Query language

s
u
o
v
z
e
l
?
r
a
s
i
P
a

n
a
r
F

select * from table

Le shell : mongo

Lecture

Slection dun document

db.collection.findOne()

Query
Slection dun document

db.collection.findOne()

Slection de documents

db.collection.find()

Slection et mise jour de document

db.collection.findAndModify()

Champ unique

{_id: azerty}

Champ unique

{_id: azerty}
{!

"_id": "azerty", !
"titre": "Enigme du vendredi", !
"activation": true!
}

Champ unique

{_id: azerty}
{!

"_id": "azerty", !
"titre": "Enigme du vendredi", !
"activation": true!

"_id": "azerty", !
"activation": true,!
"titre": "Enigme du vendredi"!

{!

Champ unique

{_id: azerty}
{!

"_id": "azerty", !
"titre": "Enigme du vendredi", !
"activation": true!

"_id": "azerty", !
"activation": true,!
"titre": "Enigme du vendredi"!

"_id": "azerty12345", !
"activation": true,!
"titre": "Enigme du vendredi"!

{!

}
{!

Sous document spcifique

{auteur: {nom: Wursteisen}}

Sous document spcifique

{auteur: {nom: Wursteisen}}


{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen"}!
}

Sous document spcifique

{auteur: {nom: Wursteisen}}


{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen"}!
}

{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen", "prnom":"David"}!
}

Sous document spcifique

{auteur: {prnom: David, nom: Wursteisen}}

Sous document spcifique

{auteur: {prnom: David, nom: Wursteisen}}


{!
"_id": "azerty", !
"auteur": {"prnom":"David", "nom":"Wursteisen"}!
}

Sous document spcifique

{auteur: {prnom: David, nom: Wursteisen}}


{!
"_id": "azerty", !
"auteur": {"prnom":"David", "nom":"Wursteisen"}!
}

{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen", "prnom":"David"}!
}

Sous champ

{auteur.nom: Wursteisen}

Sous champ

{auteur.nom: Wursteisen}
{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen"}!
}

Sous champ

{auteur.nom: Wursteisen}
{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen"}!
}

{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen", "prnom":"David"}!
}

Sous champ

{auteur.nom: Wursteisen}
{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen"}!
}

{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen", "prnom":"David"}!

}
{!
"_id": "azerty", !
"auteur": {"nom":"Bob", "prnom":"David"}!
}

Champ unique

{recouicoui: {$gt: 20}}

Champ unique

{recouicoui: {$gt: 20}}


{!
"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 50!
}

Champ unique

{recouicoui: {$gt: 20}}


{!
"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 50!

"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 10!

}
{!

Champ unique

{recouicoui: {$gt: 20}}


{!
"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 50!

"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 10!

"_id": "azerty", !
"titre": "Enigme du vendredi", !

}
{!

}
{!

!
}

criture

Insertion dun document

db.collection.insert()

Insertion dun document

db.collection.insert()

Document

Update dun document

db.collection.update(<query>, <update>)

Update dun document

db.collection.update(<query>, <update>)

modifier

Update de documents

db.collection.update(, , {multi: true})

crire un document

db.collection.update(, {prnom:David})

crire un document

db.collection.update(, {prnom:David})
{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "bob"!
}

crire un document

db.collection.update(, {prnom:David})
{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "bob"!
}

{!
"_id": "azerty", !
"prnom": "David"!
}

Modifier un champ

db.collection.update(, {$set: {prnom:David}})

Modifier un champ

db.collection.update(, {$set: {prnom:David}})


{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "bob"!
}

Modifier un champ

db.collection.update(, {$set: {prnom:David}})


{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "bob"!
}

{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "David"!
}

Incrmentation

db.collection.update(, {$inc: {recouicoui: 2 }})

Incrmentation

db.collection.update(, {$inc: {recouicoui: 2 }})


{!
"_id": "azerty", !
"nom": "wursteisen", !
"recouicoui": 5!
}

Incrmentation

db.collection.update(, {$inc: {recouicoui: 2 }})


{!
"_id": "azerty", !
"nom": "wursteisen", !
"recouicoui": 5!
}

{!
"_id": "azerty", !
"nom": "wursteisen", !
"recouicoui": 7!
}

Ajout dans un tableau

db.collection.update(, {$push: {contact: Robert }})

Ajout dans un tableau

db.collection.update(, {$push: {contact: Robert }})


{!
"_id": "azerty", !
"contact": ["John", "Bob"]
}

Ajout dans un tableau

db.collection.update(, {$push: {contact: Robert }})


{!
"_id": "azerty", !
"contact": ["John", "Bob"]

{!
"_id": "azerty", !
"contact": ["John", "Bob", "Robert"]
}

Index

Index simple

db.couicoui.ensureIndex({ name:1 })

Index compos

db.couicoui.ensureIndex({ name:1, date:-1 })

Go index

db.couicoui.ensureIndex({ geo:2d })

Index avec Time To Live

db.couicoui.ensureIndex({ name:1 }, {expireAfterSeconds: 3600})

db.find({}).explain()

{
"cursor" : "<Cursor Type and Index>",
"n" : <num>,
"nscanned" : <num>,
"scanAndOrder" : <boolean>,

Replica Set

Driver
Primary
Secondary

Secondary

Driver
criture
Primary
Secondary

Secondary

Driver
criture
Primary
Rplication
Secondary

Secondary

Driver
criture
Primary
Rplication
Secondary

Rplication
Secondary

Driver
criture
Primary
Rplication
Secondary

Lecture
Rplication
Secondary

Lecture

Driver
criture
Primary

Rplication
Secondary

Lecture
Rplication
Secondary

Primary
Secondary

Secondary

Primary
Heartbeats
Secondary

Secondary

Primary
Heartbeats
Secondary

Secondary

Primary
Secondary

Secondary

Primary
Secondary

Secondary
Primary ?

Primary
Primary

Secondary
Primary ?

Rplication

Primary

Primary

Secondary
Primary ?

Rplication

Primary

Primary

Secondary
Primary ?

Rplication

Primary
Heartbeats

Primary

Secondary
Primary ?

Rplication

Primary
Heartbeats

Primary

Secondary
Primary ?

Rplication

Secondary
Heartbeats

Primary

Secondary
Primary ?

Rplication

Secondary
Heartbeats

Primary

Secondary
Primary ?

Write Concern
notification dcriture

collection.insert(, WriteConcern.ACKNOWLEDGED);
!

db.getLastError();

(dpendant du driver)

w=1
collection.insert(, WriteConcern.ACKNOWLEDGED);
!

db.getLastError();

(dpendant du driver)

w=1
collection.insert(, WriteConcern.ACKNOWLEDGED);
!

db.getLastError();

bloquant
(dpendant du driver)

disable acknowledgment
w=0

Driver

Mongod

Driver

write
Mongod

write
Mongod

getLastError
w=0

Driver

getLastError
response

write
Mongod

getLastError
w=0

Driver

acknowledgment
w=1

Driver

Mongod

Driver

write
Mongod

write
Mongod

getLastError
w=1

Driver

write

getLastError
w=1

Driver

Mongod

apply

Mongod

apply

getLastError
response

write

getLastError
w=1

Driver

bloquant

Mongod

apply

getLastError
response

write

getLastError
w=1

Driver

journal
j=1

Driver

Mongod

Driver

write
Mongod

write
Mongod

getLastError
w=1,j=1

Driver

write

getLastError
w=1,j=1

Driver

Mongod

apply

write

getLastError
w=1,j=1

Driver

Mongod

apply

write to
journal

write to
journal

apply

getLastError
response

write

getLastError
w=1,j=1

Driver

Mongod

bloquant

write to
journal

apply

getLastError
response

write

getLastError
w=1,j=1

Driver

Mongod

replica acknowledgment
w=2

Driver

Primary

Secondary
Secondary

Driver

write
Primary

Secondary
Secondary

write
Primary

Secondary
Secondary

getLastError
w=2

Driver

write

getLastError
w=2

Driver

Primary

apply
Secondary
Secondary

write

getLastError
w=2

Driver

apply
Secondary
Secondary

replicate

Primary

write

getLastError
w=2

Driver

apply
Secondary
Secondary

replicate

Primary

apply

Secondary

replicate

Primary

getLastError
response

write
Secondary

getLastError
w=2

Driver

apply

Secondary

replicate

Primary

getLastError
response

Driver

getLastError
w=2

write
Secondary

bloquant

Secondary

replicate

apply

replicate

Primary

getLastError
response

Driver

getLastError
w=2

write
Secondary

bloquant

Sharding

Rpartition

collection

collection

Shard1

Shard2

Shard3

collection

Shard1
{x: min}

Shard2
{x: -11}

Shard3
{x: 50}

{x: max}

Avec une cl de sharding

db.collect.find({key: })
Mongos

db.collect.find({key: })
Mongos

db.collect.find({key: })
Mongos

db.collect.find({key: })
Mongos

db.collect.find({key: })
Mongos

Avec une cl non shard

db.collect.find({other: })
Mongos

db.collect.find({other: })
Mongos

db.collect.find({other: })
Mongos

Question :

Cl de sharding pour grer


les messages dun utilisateur ?

Rpartition
Cardinalit
Isolation
en criture
_id

Fiabilit

ObjectId(507f1f77bcf86cd799439011)

http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Timestamp
ObjectId(507f1f77bcf86cd799439011)

http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Timestamp
ObjectId(507f1f77bcf86cd799439011)

Host
http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Timestamp

PID

ObjectId(507f1f77bcf86cd799439011)

Host
http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Timestamp

PID

ObjectId(507f1f77bcf86cd799439011)

Host

Compteur

http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Timestamp
ObjectId(507f1f77bcf86cd799439011)

http://www.mongodb.com/presentations/advanced-sharding-features-mongodb-24

Rpartition
Cardinalit
Isolation
en criture
_id

Fiabilit

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

_id
hash(_id)

Rpartition
Cardinalit
Isolation
en criture

Fiabilit

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

Rpartition
Cardinalit
Isolation
en criture
_id
hash(_id)
user

Fiabilit

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

Rpartition
Cardinalit
Isolation
en criture
_id
hash(_id)
user
user, time

Fiabilit

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.insert({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

db.collect.find({})
Mongos

Aller plus loin

https://education.mongodb.com

http://www.meetup.com/Paris-MongoDB-User-Group/

Demo

https://github.com/dwursteisen/atelier-mongodb

Questions ?
@dwursteisen

Crdits photos

http://bit.ly/HNvkfZ
http://bit.ly/1hTARzS

http://bit.ly/HGb1BO

http://bit.ly/1hrvZ7z
http://bit.ly/1d5OVTr

http://bit.ly/17SGXwC

http://bit.ly/1awf0XL

http://bit.ly/HNvRP4
http://bit.ly/MMaRr8

http://bit.ly/HGf2Gh

http://bit.ly/1967kv2

http://bit.ly/1c6uCUA

Vous aimerez peut-être aussi