Contenu connexe Similaire à 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01-rev. (20) 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01-rev.2. 2
• Session Précédente : Rappel
• MongoDB Inserts & Queries
– ObjectId
– Récupération des Documents & Cursors
– Projections
• MongoDB Update
– Fixed Buckets
– Pre Aggregated Reports
• Write Concern
– Compromis : Durabilité / Performance
Agenda
5. 5
• Architecture de l‟Application
– JSON / RESTful
– Basé sur Python
Architecture
Client-side
JSON
(eg AngularJS) (BSON)
Pymongo driver
Python web
app
HTTP(S) REST
7. 7
Modèle : Articles
• Creation d‟articles
• Insert
• Liste d‟articles
• Renvois d‟un Curseur
• Article Unique
{
'_id' : ObjectId(...),
'text': 'Article content…',
'date' : ISODate(...),
'title' : ‟Intro to MongoDB',
'author' : 'Dan Roberts',
'tags' : [ 'mongodb',
'database',
'nosql‟
]
}
Collection : Articles
METHODES
def get_article(article_id)
def get_articles():
def create_article():
8. 8
Modèle : Comments
• Stockage des commentaires
• Récupération des
commentaires
• Ajout nouveau commentaire au
document
• „Bucketing‟
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„page‟ : 1,
„count‟ : 42
„comments‟ : [
{
„text‟ : „A great
article, helped me understand
schema design‟,
„date‟ : ISODate(..),
„author‟ : „johnsmith‟
},
…
}
Collection : Comments
METHODES
def add_comment(article_id):
def get_comments(article_id):
9. 9
Modèle : Interactions
• Reporting
• Used for reporting on
articles
• Création de rapports
“pre-aggregé”
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟,
„date‟ : ISODate(..),
„daily‟: { „views‟ : 45,
„comments‟ : 150 }
„hours‟ : {
0 : { „views‟ : 10 },
1 : { „views‟ : 2 },
…
23 : { „views‟ : 14,
„comments‟ : 10 }
}
}
Collection : Interactions
METHODES def add_interaction(article_id, type):
11. 11
>db.articles.insert({
'text': 'Article content…‟,
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : 'Dan Roberts‟,
'tags' : [ 'mongodb',
'database',
'nosql‟
]
});
• Driver génère ObjectId() pour le _id
– Si non spécifié par l‟application
– 12 octets- 4-octets epoch, 3-octets machine id, a 2-octets process id, 3-octets
counter.
Ajout de documents
12. 12
$gt, $gte, $in, $lt, $lte, $ne, $nin
• Utilisé pour requêter la base de données
• Logique: $or, $and, $not, $nor Element: $exists, $type
• Evalué: $mod, $regex, $where Geospatial: $geoWithin, $geoIntersects, $near, $nearSphere
Opérateurs: Comparaison
db.articles.find( { 'title' : ‟Intro to MongoDB‟ } )
db.articles.find( { ‟date' : { „$lt‟ :
{ISODate("2014-02-19T00:00:00.000Z") }} )
db.articles.find( { „tags‟ : { „$in‟ : [„nosql‟, „database‟] } } );
13. 13
• Find retourne un curseur
– Utilisé pour naviguer dans le résultat
– Un curseur a plusieurs méthodes
Curseurs
>var cursor = db.articles.find ( { ‟author' : ‟Tug Grall‟ } )
>cursor.hasNext()
true
>cursor.next()
{ '_id' : ObjectId(...),
'text': 'Article content…‟,
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : 'Dan Roberts‟,
'tags' : [ 'mongodb', 'database‟, 'nosql’ ]
}
14. 14
• Retourne uniquement certains attributs
– Booléen 0/1 pour sélectionner les attributs
– Plus efficace
Projections
>var cursor = db.articles.find( { ‟author' : ‟Tug Grall‟ } , {‘_id’:0, ‘title’:1})
>cursor.hasNext()
true
>cursor.next()
{ "title" : "Intro to MongoDB" }
16. 16
$each, $slice, $sort, $inc, $push
$inc, $rename, $setOnInsert, $set, $unset, $max, $min
$, $addToSet, $pop, $pullAll, $pull, $pushAll, $push
$each, $slice, $sort
Opérateur : Update
>db.articles.update(
{ '_id' : ObjectId(...)},
{ '$push' :
{'comments' : „Great
article!’ }
}
)
{ 'text': 'Article content…‟
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : ‟Tug Grall‟,
'tags' : ['mongodb',
'database‟,'nosql’ ],
’comments' :
[‘Great article!’ ]
}
17. 17
Ajout d’élément à un tableau
$push, $each, $slice
Opérateur : Update
>db.articles.update(
{ '_id' : ObjectId(...)},
{ '$push' : {'comments' :
{
'$each' : [„Excellent‟],
'$slice' : -3}},
})
{ 'text': 'Article content…‟
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : 'Dan Roberts‟,
'tags' : ['mongodb',
'database‟,'nosql’ ],
’comments' :
[‘Great
article!’, ‘More
please’, ‘Excellent’ ]
}
18. 18
• Ajout de commentaires dans un document (max : 10 - bucket).
• Création d‟un nouveau.
• Utilisation de {upsert: true} .
Opérateur : Update- Bucketing
>db.comments.update(
{„c‟: {„$lt‟:10}},
{
„$inc‟ : {c:1},
'$push' : {
'comments' :
„Excellent‟ }
},
{ upsert : true }
)
{
„_id‟ : ObjectId( … )
„c‟ : 3,
’comments' :
[‘Great article!’,
‘More please’,
‘Excellent’ ]
}
19. 19
Analytique– Pre-Agrégation
• Reporting
• Rapports Pré-agregés
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟,
„date‟ : ISODate(..),
„daily‟: { „views‟ : 45,
„comments‟ : 150 }
„hours‟ : {
0 : { „views‟ : 10 },
1 : { „views‟ : 2 },
…
23 : { „views‟ : 14,
„comments‟ : 10 }
}
}
Collections : Interactions
METHODE def add_interaction(article_id, type):
20. 20
• Utilisation de $inc pour incrémenter plusieurs compteurs.
• Opération atomique
• Incrémentation des compteurs par jour et heure
Compteurs : Incrément
>db.interactions.update(
{„article_id‟ : ObjectId(..)},
{
„$inc‟ : {
„daily.views‟:1,
„daily.comments‟:1
„hours.8.views‟:1
„hours.8.comments‟:1
}
)
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟,
„date‟ : ISODate(..),
„daily‟: { „views‟ : 45,
„comments‟ : 150 }
„hours‟ : {
0 : { „views‟ : 10 },
1 : { „views‟ : 2 },
…
23 : { „views‟ : 14,
„comments‟ : 10 }
}
}
21. 21
• Création de nouveaux compteurs
Compteurs : Incrément (2)
>db.interactions.update(
{„article_id‟ : ObjectId(..)},
{
„$inc‟ : {
„daily.views‟:1,
„daily.comments‟:1,
„hours.8.views‟:1,
„hours.8.comments‟:1,
‘referrers.bing’ : 1
}
)
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟,
„date‟ : ISODate(..),
„daily‟: { „views‟ : 45,
„comments‟ : 150 }
„hours‟ : {
…..
}
„referrers‟ : {
„google‟ : 27
}
}
22. 22
• Increment new counters
Compteurs : Incrément (2)
>db.interactions.update(
{„article_id‟ : ObjectId(..)},
{
„$inc‟ : {
„daily.views‟:1,
„daily.comments‟:1,
„hours.8.views‟:1,
„hours.8.comments‟:1,
‘referrers.bing’ : 1
}
)
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟,
„date‟ : ISODate(..),
„daily‟: { „views‟ : 45,
„comments‟ : 150 }
„hours‟ : {
…..
}
„referrers‟ : {
„google‟ : 27,
‘bing’ : 1
}
}
24. 24
Durabilité
• Avec MongoDB, plusieurs options
• Memoire/RAM
• Disque (primaire)
• Plusieurs serveur (replicats)
• Write Concerns
• Retour sur le status de l‟opération d‟écriture
• getLastError() appelé par le driver
• Compromis
• Latence
28. 28
Replica Sets
• Replica Set – 2 copies ou plus
• Tolérant aux pannes
• Répond à plusieurs contraintes:
- High Availability
- Disaster Recovery
- Maintenance
31. 31
• Interactions
– Requtes et projections
– Inserts & Upserts
– Opérateurs : Update
– Bucketing
– Rapports pre-agrégés
• Base pour les rapport analytiques
Résumé
35. Risorsa WEBSITE URL
Enterprise Download mongodb.com/download
Training Online Gratuito education.mongodb.com
Webinars e Events mongodb.com/events
White Paper mongodb.com/white-papers
Casi d‟Uso mongodb.com/customers
Presentazioni mongodb.com/presentations
Documentazione docs.mongodb.org
Notes de l'éditeur Not really fire and forget. This return arrow is to confirm that the network successfully transferred the packet(s) of data.This confirms that the TCP ACK response was received. Presenter should mention:Default is w:1w:majority is what most people should use for durability. Majority is a special token here signifying more than half of the nodes in the set have acknowledged the write.