Académique Documents
Professionnel Documents
Culture Documents
11/28/13
@dwursteisen
!1
MongoDB
Big Database
@DWURSTEISEN
http://fr.slideshare.net/soatexpert
WARNING
20:00:00
Ce site dboite
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
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
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
http://docs.mongodb.org/manual/reference/bson-types/
Organiser
les donnes selon leurs
Utilisations
ci gt la
4me forme
normale
Question :
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,
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,
recouicoui: 2, // compteur
_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,
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,
> 16Mb
Information partielle
{
_id: ObjectId(11),
user: {mail: user1, avatar: http://.,
content: blabla,
recouicoui: {
users: [{user: {} * 5],
compteur: 789013
}
Le design dune
bonne
complexe
Query language
s
u
o
v
z
e
l
?
r
a
s
i
P
a
n
a
r
F
Le shell : mongo
Lecture
db.collection.findOne()
Query
Slection dun document
db.collection.findOne()
Slection de documents
db.collection.find()
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"!
{!
}
{!
{!
"_id": "azerty", !
"auteur": {"nom":"Wursteisen", "prnom":"David"}!
}
{!
"_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
Champ unique
Champ unique
"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 10!
}
{!
Champ unique
"_id": "azerty", !
"titre": "Enigme du vendredi", !
"recouicoui": 10!
"_id": "azerty", !
"titre": "Enigme du vendredi", !
}
{!
}
{!
!
}
criture
db.collection.insert()
db.collection.insert()
Document
db.collection.update(<query>, <update>)
db.collection.update(<query>, <update>)
modifier
Update de documents
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
Modifier un champ
Modifier un champ
{!
"_id": "azerty", !
"nom": "wursteisen", !
"prnom": "David"!
}
Incrmentation
Incrmentation
Incrmentation
{!
"_id": "azerty", !
"nom": "wursteisen", !
"recouicoui": 7!
}
{!
"_id": "azerty", !
"contact": ["John", "Bob", "Robert"]
}
Index
Index simple
db.couicoui.ensureIndex({ name:1 })
Index compos
Go index
db.couicoui.ensureIndex({ geo:2d })
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}
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
db.collect.find({other: })
Mongos
db.collect.find({other: })
Mongos
db.collect.find({other: })
Mongos
Question :
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
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