SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Serie Sviluppo di un’Applicazione
Back to Basics
Interagire con il Database
Senior Solutions Architect, MongoDB Inc.
Massimo Brignoli
#MongoDBBasicsIT
Agenda
• Riassunto della lezione precedente
• Gli Operatori di Query di MongoDB
– Restituire documenti - cursori
– Proiezioni
• Gli Operatori di Update di MongoDB
– Pacchetti Fissi
– Report PreAggregati
• Write Concern
– Compromesso tra Durabilità e Velocità
Q & A
• Virtual Genius Bar
– Usate la chat per fare
domande
– Il team italiano vi
risponderà
– Usatelo durante la
sessione!
Riassunto dell’Ultima Lezione
Abbia visto l‟architettura applicativa
– JSON / RESTful
– Basata su Python
Schema design
– Modellato
• Articoli
• Commenti
• Interazioni
Schema e Architectura
Client-side
JSON
(eg AngularJS) (BSON)Pymongo
driver
Python
web app
HTTP(S) REST
Modellazione degli Articoli
• Post degli articoli
• Ottenere la lista degli
articoli
• Ritornare un cursore
• Ottenere un articolo
individuale
{
'_id' : ObjectId(...),
'text': 'Article content…',
'date' : ISODate(...),
'title' : ‟Intro to MongoDB',
'author' : 'Dan Roberts',
'tags' : [ 'mongodb',
'database',
'nosql‟
]
}
Collection degli articoli
METODI
def get_article(article_id)
def get_articles():
def create_article():
Modellazione dei Commenti
• Memorizzare i commenti
• Ottenere velocemente i
commenti più recenti
• Aggiungere nuovi
commenti a un
documento
• „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 dei
commentiMETODI
def add_comment(article_id):
def get_comments(article_id):
Modellazione delle Interazioni
• Usato per reportistica
sugli articoli
• Creazione di report
“pre-aggregati”
{
„_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 interazioni
METPDI def add_interaction(article_id, type):
Eseguire le Query
$gt, $gte, $in, $lt, $lte, $ne, $nin
Usati per cercare nei documenti
– Array inclusi
Gli Operatori delle Query
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‟] } } );
La Find restituisce un cursore
– Usato per iterate il set dei risultati
– Il cursore ha molti metodi
Cursori
>var cursor = db.articles.find( { ‟author' : ‟Dan Roberts‟ } )
>cursor.hasNext()
true
>cursor.next()
{ '_id' : ObjectId(...),
'text': 'Article content…‟,
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : 'Dan Roberts‟,
'tags' : [ 'mongodb', 'database‟,'nosql’ ]
}
Restituiscono solamente gli attributi necessari
– La sintassi per selezionare gli attributi usa i booleani 0 e 1
– Aumenta l‟efficienza
Proiezioni
>var cursor = db.articles.find( { ‟author' : ‟Dan Roberts‟ } , {‘_id’:0, ‘title’:1})
>cursor.hasNext()
true
>cursor.next()
{ "title" : "Intro to MongoDB" }
Update dei dati
$each, $slice, $sort, $inc, $push
$rename,$setOnInsert, $set, $unset,$max, $min
$addToSet,$pop,$pullAll,$pull,$pushAll,$push
Operatori di Update
>db.articles.update(
{ '_id' : ObjectId(...)},
{ '$push' :
{'comments' : „Great
article!‟}
}
)
{ 'text': 'Article content…‟
'date' : ISODate(...),
'title' : ‟Intro to MongoDB‟,
'author' : 'Dan Roberts‟,
'tags' : ['mongodb',
'database‟,'nosql’ ],
’comments' :
[‘Great article!’ ]
}
Aggiungere un elementoad un array di lunghezza fissa con…
$push, $each, $slice
Operatori di 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’ ]
}
Operatori di Update - Bucketing
• Scriviamo 10 commenti in un singolo documento (bucket)
• Crea automaticamente un nuovo documento
• Use{upsert: true} not insert.>db.comments.update(
{„c‟: {„$lt‟:10}},
{
„$inc‟ : {c:1},
'$push' : {
'comments' :
„Excellent‟ }
},
{upsert : true}
)
{
„_id‟ : ObjectId( … )
„c‟ : 3,
’comments' :
[‘Great article!’,
‘More please’,
‘Excellent’ ]
}
Analitica– Report Pre-Aggregati
• Usato per fare
statistiche sugli
articoli
• Crea report pre-
aggregati
{
„_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 Interazioni
METODO def add_interaction(article_id, type):
Incrementare i Contatori
• Usate $inc per incrementare contatorimultipli
• Singola operazioneAtomica
• Incrementai contatori giornalieri e orari
>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 }
}
}
Incrementare i Contatori
• Incrementate nuovi contatori
>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
}
}
Incrementare i Contatori
• Incrementate nuovi contatori
>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
}
}
Durabilità
Durabilità
• Con MongoDB potete scegliere
• In memoria
• Su disco
• Su multipli server
• Write Concern
• Ritorna dopo il successo di un‟operazione di scrittura
• Il driver chiama getLastError
• Compromesso
• Latenza della risposta
Unacknowledged
MongoDB Acknowledged
Wait for Journal Sync
Replica Set
• Replica Set – due o più copie
• E‟ uno shard “Self-healing”
• Indirizza diverse esigenze:
- High Availability
- Disaster Recovery
- Manutenzione
Wait for Replication
Riassunto
Riassumendo
• Interagendo con il database
– Query e proiezioni
– Insert e Upsert
– Operatori
– Bucketing
– Report PreAggregated
Prossima Sessione: 29 Aprile.
– Indicizzazione
• Strategie di indicizzazione
• Search testuale
• Ricerche geografiche
– Esempi di Codice
2014   it - app dev series - 03 - interagire con il database

Weitere ähnliche Inhalte

Was ist angesagt?

ピグライフ と node.js
ピグライフ と node.jsピグライフ と node.js
ピグライフ と node.jsSuguru Namura
 
Introduction to Service Worker
Introduction to Service WorkerIntroduction to Service Worker
Introduction to Service WorkerShogo Sensui
 
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)Muhammad Yusuf
 
Node.js: scalability tips - CityJS 2020
Node.js: scalability tips - CityJS 2020Node.js: scalability tips - CityJS 2020
Node.js: scalability tips - CityJS 2020Luciano Mammino
 
"><img src=x onerror=prompt(document.domain)>
"><img src=x onerror=prompt(document.domain)>"><img src=x onerror=prompt(document.domain)>
"><img src=x onerror=prompt(document.domain)>Jaymark Pestaño
 
ECMAScript 6 im Produktivbetrieb
ECMAScript 6 im ProduktivbetriebECMAScript 6 im Produktivbetrieb
ECMAScript 6 im ProduktivbetriebSebastian Springer
 
Collection pipeline par Mathieu Godart
Collection pipeline par  Mathieu GodartCollection pipeline par  Mathieu Godart
Collection pipeline par Mathieu GodartCocoaHeads France
 
Javascript4
Javascript4Javascript4
Javascript4mozks
 
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017devCAT Studio, NEXON
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jqueryAdam Zygadlewicz
 

Was ist angesagt? (16)

ピグライフ と node.js
ピグライフ と node.jsピグライフ と node.js
ピグライフ と node.js
 
Proxy & CGLIB
Proxy & CGLIBProxy & CGLIB
Proxy & CGLIB
 
Introduction to Service Worker
Introduction to Service WorkerIntroduction to Service Worker
Introduction to Service Worker
 
Core Data
Core DataCore Data
Core Data
 
es6.concurrency()
es6.concurrency()es6.concurrency()
es6.concurrency()
 
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)
Praktik Pengembangan Konten HTML5 untuk E-Learning (Extended)
 
Node.js: scalability tips - CityJS 2020
Node.js: scalability tips - CityJS 2020Node.js: scalability tips - CityJS 2020
Node.js: scalability tips - CityJS 2020
 
Blockly
BlocklyBlockly
Blockly
 
"><img src=x onerror=prompt(document.domain)>
"><img src=x onerror=prompt(document.domain)>"><img src=x onerror=prompt(document.domain)>
"><img src=x onerror=prompt(document.domain)>
 
ECMAScript 6 im Produktivbetrieb
ECMAScript 6 im ProduktivbetriebECMAScript 6 im Produktivbetrieb
ECMAScript 6 im Produktivbetrieb
 
Load3
Load3Load3
Load3
 
Collection pipeline par Mathieu Godart
Collection pipeline par  Mathieu GodartCollection pipeline par  Mathieu Godart
Collection pipeline par Mathieu Godart
 
Javascript4
Javascript4Javascript4
Javascript4
 
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017
이승재, 강성훈, 내가 만든 언어의 개발환경을 Visual Studio Code로 빠르고 쉽게 구축하기 #2, NDC2017
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquery
 
Kruskal algorithm
Kruskal algorithmKruskal algorithm
Kruskal algorithm
 

Mehr von MongoDB

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump StartMongoDB
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB
 

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

2014 it - app dev series - 03 - interagire con il database

  • 1. Serie Sviluppo di un’Applicazione Back to Basics Interagire con il Database Senior Solutions Architect, MongoDB Inc. Massimo Brignoli #MongoDBBasicsIT
  • 2. Agenda • Riassunto della lezione precedente • Gli Operatori di Query di MongoDB – Restituire documenti - cursori – Proiezioni • Gli Operatori di Update di MongoDB – Pacchetti Fissi – Report PreAggregati • Write Concern – Compromesso tra Durabilità e Velocità
  • 3. Q & A • Virtual Genius Bar – Usate la chat per fare domande – Il team italiano vi risponderà – Usatelo durante la sessione!
  • 5. Abbia visto l‟architettura applicativa – JSON / RESTful – Basata su Python Schema design – Modellato • Articoli • Commenti • Interazioni Schema e Architectura Client-side JSON (eg AngularJS) (BSON)Pymongo driver Python web app HTTP(S) REST
  • 6. Modellazione degli Articoli • Post degli articoli • Ottenere la lista degli articoli • Ritornare un cursore • Ottenere un articolo individuale { '_id' : ObjectId(...), 'text': 'Article content…', 'date' : ISODate(...), 'title' : ‟Intro to MongoDB', 'author' : 'Dan Roberts', 'tags' : [ 'mongodb', 'database', 'nosql‟ ] } Collection degli articoli METODI def get_article(article_id) def get_articles(): def create_article():
  • 7. Modellazione dei Commenti • Memorizzare i commenti • Ottenere velocemente i commenti più recenti • Aggiungere nuovi commenti a un documento • „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 dei commentiMETODI def add_comment(article_id): def get_comments(article_id):
  • 8. Modellazione delle Interazioni • Usato per reportistica sugli articoli • Creazione di report “pre-aggregati” { „_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 interazioni METPDI def add_interaction(article_id, type):
  • 10. $gt, $gte, $in, $lt, $lte, $ne, $nin Usati per cercare nei documenti – Array inclusi Gli Operatori delle Query 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‟] } } );
  • 11. La Find restituisce un cursore – Usato per iterate il set dei risultati – Il cursore ha molti metodi Cursori >var cursor = db.articles.find( { ‟author' : ‟Dan Roberts‟ } ) >cursor.hasNext() true >cursor.next() { '_id' : ObjectId(...), 'text': 'Article content…‟, 'date' : ISODate(...), 'title' : ‟Intro to MongoDB‟, 'author' : 'Dan Roberts‟, 'tags' : [ 'mongodb', 'database‟,'nosql’ ] }
  • 12. Restituiscono solamente gli attributi necessari – La sintassi per selezionare gli attributi usa i booleani 0 e 1 – Aumenta l‟efficienza Proiezioni >var cursor = db.articles.find( { ‟author' : ‟Dan Roberts‟ } , {‘_id’:0, ‘title’:1}) >cursor.hasNext() true >cursor.next() { "title" : "Intro to MongoDB" }
  • 14. $each, $slice, $sort, $inc, $push $rename,$setOnInsert, $set, $unset,$max, $min $addToSet,$pop,$pullAll,$pull,$pushAll,$push Operatori di Update >db.articles.update( { '_id' : ObjectId(...)}, { '$push' : {'comments' : „Great article!‟} } ) { 'text': 'Article content…‟ 'date' : ISODate(...), 'title' : ‟Intro to MongoDB‟, 'author' : 'Dan Roberts‟, 'tags' : ['mongodb', 'database‟,'nosql’ ], ’comments' : [‘Great article!’ ] }
  • 15. Aggiungere un elementoad un array di lunghezza fissa con… $push, $each, $slice Operatori di 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’ ] }
  • 16. Operatori di Update - Bucketing • Scriviamo 10 commenti in un singolo documento (bucket) • Crea automaticamente un nuovo documento • Use{upsert: true} not insert.>db.comments.update( {„c‟: {„$lt‟:10}}, { „$inc‟ : {c:1}, '$push' : { 'comments' : „Excellent‟ } }, {upsert : true} ) { „_id‟ : ObjectId( … ) „c‟ : 3, ’comments' : [‘Great article!’, ‘More please’, ‘Excellent’ ] }
  • 17. Analitica– Report Pre-Aggregati • Usato per fare statistiche sugli articoli • Crea report pre- aggregati { „_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 Interazioni METODO def add_interaction(article_id, type):
  • 18. Incrementare i Contatori • Usate $inc per incrementare contatorimultipli • Singola operazioneAtomica • Incrementai contatori giornalieri e orari >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 } } }
  • 19. Incrementare i Contatori • Incrementate nuovi contatori >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 } }
  • 20. Incrementare i Contatori • Incrementate nuovi contatori >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 } }
  • 22. Durabilità • Con MongoDB potete scegliere • In memoria • Su disco • Su multipli server • Write Concern • Ritorna dopo il successo di un‟operazione di scrittura • Il driver chiama getLastError • Compromesso • Latenza della risposta
  • 26. Replica Set • Replica Set – due o più copie • E‟ uno shard “Self-healing” • Indirizza diverse esigenze: - High Availability - Disaster Recovery - Manutenzione
  • 29. Riassumendo • Interagendo con il database – Query e proiezioni – Insert e Upsert – Operatori – Bucketing – Report PreAggregated
  • 30. Prossima Sessione: 29 Aprile. – Indicizzazione • Strategie di indicizzazione • Search testuale • Ricerche geografiche – Esempi di Codice

Hinweis der Redaktion

  1. 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.
  2. 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.