2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01
Transcript of 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01
![Page 1: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/1.jpg)
Session 3 – Interaction
avec la base de données
Application Development Series
Bienvenue à la session 3
À PARTIR DE
15:00
CET
![Page 2: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/2.jpg)
Application Development Series:
Session 3 – Interaction avec la
base de données
Présenté par
Tugdual Grall
Solutions Architect
MongoDB
Webinar Series:Premiers Pas avec MongoDB
À PARTIR DE
MINUTES
05
![Page 3: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/3.jpg)
About MetLifeMetLife, Inc. is a leading global provider of
insurance, annuities and employee benefit
programs, serving 90 million customers. Through
its subsidiaries and affiliates, MetLife holds leading
market positions in the United States, Japan, Latin
America, Asia, Europe and the Middle East.
Exemple d'utilisation du client
MetLife Leapfrogs Insurance Industry with MongoDB-Powered
Big Data Application
MetLife, Inc. selected MongoDB as the data engine for “The Wall”, an innovative customer
service application. Similar to the Facebook User Interface, The Wall provides a 360-
degree, consolidated view of MetLife customers, including policy details and transactions
across lines of business. The Wall improves customer satisfaction and boosts call centre
productivity.
MetLife built a working prototype in two weeks and was live in U.S. call centres in just 90
days. Currently, The Wall handles 45 million agreements with 140 million transactions.
By surfacing customer data in The Wall, MetLife has shown how global companies can
simultaneously leverage the data from 70+ existing systems with the flexibility of emerging
technology. The MetLife team employed both traditional and modern technologies,
exhibiting agility in applying solutions to dynamic needs.
Webinar Series:
Premiers Pas
avec MongoDB
Application
Development Series:
Session 3:
Interaction avec la
base de données
04
À PARTIR DE
MINUTES
![Page 4: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/4.jpg)
Alcuni dei nostri clienti…
Webinar Series:
Premiers Pas
avec MongoDB
Application
Development Series:
Session 3:
Interaction avec la
base de données
03
À PARTIR DE
MINUTES
![Page 5: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/5.jpg)
About Orange DigitalOrange Digital is a subsidiary of France Telecom -
Orange supplying digital services to EE in the UK
and Orange across Europe. Orange Digital maintains
the websites Orange, Orange World, and the Orange
Business site, as well as a number of EE‟s digital
assets.
Exemple d'utilisation du client
Managing such varied content is a mammoth undertaking, as
connection speeds increase and expectations grow
Under these circumstances, Orange Digital started to look for alternatives, both in terms of
database and delivery platform. After some deliberation, research and testing, the company
settled on abandoning hosting the data itself and instead moved to Amazon's cloud-based
web service.
Along with the move to Amazon, Orange Digital decided to use a non-relational database to
store content and metadata. After experimenting with several options Orange Digital chose
MongoDB due to its strong performance, ease of use but the decision was primarily due to
replication, auto sharding, failover and disaster recovery features which are especially relevant
on a cloud based infrastructure where hardware failures do happen. Orange Digital solutions
are designed to assume that failure will happen and must gracefully cope with failure.
"We tried several different databases, but the incredible performance, horizontal scalability
and automatic backup and failover functionality made MongoDB the natural choice for us -
and makes life easier for our developers," announced Orange.
02
Webinar Series:
Premiers Pas
avec MongoDB
Application
Development Series:
Session 3:
Interaction avec la
base de données
À PARTIR DE
MINUTES
![Page 6: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/6.jpg)
Application Development Series:
Session 3 – Interaction avec la
base de données
Présenté par
Tugdual Grall
Solutions Architect
MongoDB
Webinar Series:Premiers Pas avec MongoDB
À PARTIR DE
MINUTES
01
![Page 7: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/7.jpg)
Application Development SeriesBack to Basics
Interaction avec la base de données
Tugdual Grall
@tgrall
#MongoDBBasics
![Page 8: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/8.jpg)
8
• 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
![Page 9: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/9.jpg)
9
• Virtual Genius Bar
– Utilisez la fenêtre de chat
Q & A
![Page 10: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/10.jpg)
Recap from last time….
![Page 11: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/11.jpg)
11
• Architecture de l‟Application
– JSON / RESTful
– Basé sur Python
Architecture
Client-side
JSON(eg AngularJS) (BSON)
Pymongo driver
Python web
appHTTP(S) REST
![Page 12: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/12.jpg)
12
• Design• Articles
• Comments
• Interactions
• Users
Schema & Architecture
![Page 13: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/13.jpg)
13
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():
![Page 14: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/14.jpg)
14
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
METHODESdef add_comment(article_id):
def get_comments(article_id):
![Page 15: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/15.jpg)
15
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):
![Page 16: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/16.jpg)
Création / Requêtes
![Page 17: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/17.jpg)
17
>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
![Page 18: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/18.jpg)
18
$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‟] } } );
![Page 19: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/19.jpg)
19
• 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’ ]
}
![Page 20: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/20.jpg)
20
• 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" }
![Page 21: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/21.jpg)
Mises à jour
![Page 22: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/22.jpg)
22
$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!’ ]
}
![Page 23: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/23.jpg)
23
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’ ]
}
![Page 24: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/24.jpg)
24
• 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’ ]
}
![Page 25: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/25.jpg)
25
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):
![Page 26: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/26.jpg)
26
• 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 }
}
}
![Page 27: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/27.jpg)
27
• 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
}
}
![Page 28: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/28.jpg)
28
• 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
}
}
![Page 29: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/29.jpg)
Durabilité
![Page 30: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/30.jpg)
30
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
![Page 31: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/31.jpg)
31
Unacknowledged
![Page 32: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/32.jpg)
32
MongoDB Acknowledged
Default Write Concern
![Page 33: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/33.jpg)
33
Wait for Journal Sync
![Page 34: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/34.jpg)
34
Replica Sets
• Replica Set – 2 copies ou plus
• Tolérant aux pannes
• Répond à plusieurs contraintes:
- High Availability
- Disaster Recovery
- Maintenance
![Page 35: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/35.jpg)
35
Wait for Replication
![Page 36: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/36.jpg)
Résumé
![Page 37: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/37.jpg)
37
• Interactions
– Requtes et projections
– Inserts & Upserts
– Opérateurs : Update
– Bucketing
– Rapports pre-agrégés
• Base pour les rapport analytiques
Résumé
![Page 38: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/38.jpg)
38
– Indexation
• Stratégies/Options
• Optimisation
– Text Search
– Geo Spatial
– Query Profiler
Prochaine Session – 9 Avril
![Page 39: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/39.jpg)
![Page 40: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/40.jpg)
Tweet vos questions à
#mongoDBBasics
![Page 41: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/41.jpg)
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
![Page 42: 2014 03-26-appdevseries-session3-interactingwiththedatabase-fr-phpapp01](https://reader034.fdocuments.in/reader034/viewer/2022042817/55a2fef31a28abf8358b464d/html5/thumbnails/42.jpg)