SlideShare uma empresa Scribd logo
1 de 27
16 de abril FLISOL 2016 1
MongoDBMongoDB
Not Only SQLNot Only SQL
16 de abril FLISOL 2016 2
Lucas de OliveiraLucas de Oliveira
http://deoliveiralucas.nethttp://deoliveiralucas.net
16 de abril FLISOL 2016 3
•
NoSQL = Not Only SQL
•
Alto desempenho
•
Diferentes modelos de dados (documentos,
grafos, chave-valor e colunas)
NoSQLNoSQL
16 de abril FLISOL 2016 4
•
JSON/BSON
•
Schemaless
•
Replica Set
MongoDBMongoDB
16 de abril FLISOL 2016 5
•
Sharding
•
GridFS
•
Geolocation
MongoDBMongoDB
16 de abril FLISOL 2016 6
ExemploExemplo:
Banco de dados relacional
MongoDBMongoDB
IdId nomenome emailemail
1 Aluno1 email1@email.com
2 Aluno2 email2@email.com
Tabela de alunos
idid id_alunoid_aluno telefonetelefone
1 1 1234-5678
2 1 1234-7070
3 2 9101-1121
Tabela de telefones
16 de abril FLISOL 2016 7
ExemploExemplo:
Não relacional orientado a documentos
MongoDBMongoDB
{
nome: Aluno1,
email: email1@email.com,
telefones: [ 1234-5678, 1234-7070 ]
}
{
nome: Aluno2,
email: email2@email.com,
telefones: [ 9101-1121 ]
}
16 de abril FLISOL 2016 8
> mongoimport --db unisal --collection posts --file posts.json
> mongoimport --db unisal --collection grades --file grades.json
• --batchSize 1
> mongoexport --db unisal --collection posts --out posts.json
MongoDB – Import / ExportMongoDB – Import / Export
16 de abril FLISOL 2016 9
> mongod
> mongo
> show dbs
> use unisal
> show collections
MongoDB – AcessoMongoDB – Acesso
16 de abril FLISOL 2016 10
> db.alunos.insert({
nome: "Lucas",
sobrenome: "de Oliveira",
email: "contato@deoliveiralucas.net"
})
MongoDB - InserirMongoDB - Inserir
16 de abril FLISOL 2016 11
> var aluno = {
nome: "de Oliveira",
sobrenome: "Lucas",
email: "deoliveiralucas@contato.net"
}
> db.alunos.insert(aluno)
MongoDB - InserirMongoDB - Inserir
16 de abril FLISOL 2016 12
> var query = {
nome: "Lucas"
}
> db.alunos.find(query)
MongoDB - ConsultarMongoDB - Consultar
16 de abril FLISOL 2016 13
OperadoresOperadores (Comparação)(Comparação)
•
$eq
•
$gt
•
$gte
•
$lt
MongoDB - ConsultarMongoDB - Consultar
16 de abril FLISOL 2016 14
OperadoresOperadores (Comparação)(Comparação)
•
$lte
•
$ne
•
$in
•
$nin
MongoDB - ConsultarMongoDB - Consultar
16 de abril FLISOL 2016 15
OperadoresOperadores (Lógico)(Lógico)
•
$or
•
$and
•
$not
•
$nor
MongoDB - ConsultarMongoDB - Consultar
16 de abril FLISOL 2016 16
•
Map-Reduce
•
Aggregation
MongoDB - ConsultarMongoDB - Consultar
16 de abril FLISOL 2016 17
> var query = {
nome: "Lucas"
}
> var mod = {
nome: "Oliveira"
}
> db.alunos.update(query, mod)
MongoDB - AtualizarMongoDB - Atualizar
16 de abril FLISOL 2016 18
Agora vamos fazer direito
> var query = {
nome: "de Oliveira"
}
> var mod = {
$set: {
nome: "deoliveiralucas"
}
}
> db.alunos.update(query, mod)
MongoDB - AtualizarMongoDB - Atualizar
16 de abril FLISOL 2016 19
Operadores (campo)Operadores (campo)
•
$set
•
$inc
•
$rename
•
$unset
•
$min
•
$max
•
$currentDate
MongoDB - AtualizarMongoDB - Atualizar
16 de abril FLISOL 2016 20
Operadores (array)Operadores (array)
•
$addToSet
•
$pop
•
$pullAll
•
$pull
•
$push
MongoDB - AtualizarMongoDB - Atualizar
16 de abril FLISOL 2016 21
> var cursor = db.alunos.find()
> while(cursor.hasNext()){ print(tojson(cursor.next())) }
MongoDB - CursorMongoDB - Cursor
16 de abril FLISOL 2016 22
for (var i = 0; i < 10000; i++) {for (var i = 0; i < 10000; i++) {
var aluno = {
nome: "nome" + i,
sobrenome: "sobrenome" + i,
idade: Math.floor((Math.random() * (80 - 15)) +
15)
};
db.alunos.insert(aluno);
}
MongoDB - JavascriptMongoDB - Javascript
16 de abril FLISOL 2016 23
> mongod --replSet unisalReplica --port 27015 --dbpath /data/rs1> mongod --replSet unisalReplica --port 27015 --dbpath /data/rs1
> mongod --replSet unisalReplica --port 27016 --dbpath /data/rs2> mongod --replSet unisalReplica --port 27016 --dbpath /data/rs2
> mongod --replSet unisalReplica --port 27017 --dbpath /data/rs3> mongod --replSet unisalReplica --port 27017 --dbpath /data/rs3
> mongod --replSet unisalReplica --port 27018 --dbpath /data/arb> mongod --replSet unisalReplica --port 27018 --dbpath /data/arb
MongoDB – Replica SetMongoDB – Replica Set
16 de abril FLISOL 2016 24
> mongo --port 27015> mongo --port 27015
> rs.initiate()
> rs.status()
> rs.add("deoliveiralucas:27016")
> rs.add("deoliveiralucas:27017")
> rs.addArb("deoliveiralucas:27018")
MongoDB – Replica SetMongoDB – Replica Set
16 de abril FLISOL 2016 25
•
MongoDB UniversityMongoDB University
•
Webschool.ioWebschool.io
MongoDB – CursosMongoDB – Cursos
16 de abril FLISOL 2016 26
•
https://docs.mongodb.org/manual/https://docs.mongodb.org/manual/
•
https://docs.mongodb.org/manual/tutorial/query-documents/https://docs.mongodb.org/manual/tutorial/query-documents/
•
https://docs.mongodb.org/manual/administration/replica-sets/https://docs.mongodb.org/manual/administration/replica-sets/
ReferênciasReferências
16 de abril FLISOL 2016 27
Obrigado!Obrigado!

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

MongoDB Introdução

  • 1. 16 de abril FLISOL 2016 1 MongoDBMongoDB Not Only SQLNot Only SQL
  • 2. 16 de abril FLISOL 2016 2 Lucas de OliveiraLucas de Oliveira http://deoliveiralucas.nethttp://deoliveiralucas.net
  • 3. 16 de abril FLISOL 2016 3 • NoSQL = Not Only SQL • Alto desempenho • Diferentes modelos de dados (documentos, grafos, chave-valor e colunas) NoSQLNoSQL
  • 4. 16 de abril FLISOL 2016 4 • JSON/BSON • Schemaless • Replica Set MongoDBMongoDB
  • 5. 16 de abril FLISOL 2016 5 • Sharding • GridFS • Geolocation MongoDBMongoDB
  • 6. 16 de abril FLISOL 2016 6 ExemploExemplo: Banco de dados relacional MongoDBMongoDB IdId nomenome emailemail 1 Aluno1 email1@email.com 2 Aluno2 email2@email.com Tabela de alunos idid id_alunoid_aluno telefonetelefone 1 1 1234-5678 2 1 1234-7070 3 2 9101-1121 Tabela de telefones
  • 7. 16 de abril FLISOL 2016 7 ExemploExemplo: Não relacional orientado a documentos MongoDBMongoDB { nome: Aluno1, email: email1@email.com, telefones: [ 1234-5678, 1234-7070 ] } { nome: Aluno2, email: email2@email.com, telefones: [ 9101-1121 ] }
  • 8. 16 de abril FLISOL 2016 8 > mongoimport --db unisal --collection posts --file posts.json > mongoimport --db unisal --collection grades --file grades.json • --batchSize 1 > mongoexport --db unisal --collection posts --out posts.json MongoDB – Import / ExportMongoDB – Import / Export
  • 9. 16 de abril FLISOL 2016 9 > mongod > mongo > show dbs > use unisal > show collections MongoDB – AcessoMongoDB – Acesso
  • 10. 16 de abril FLISOL 2016 10 > db.alunos.insert({ nome: "Lucas", sobrenome: "de Oliveira", email: "contato@deoliveiralucas.net" }) MongoDB - InserirMongoDB - Inserir
  • 11. 16 de abril FLISOL 2016 11 > var aluno = { nome: "de Oliveira", sobrenome: "Lucas", email: "deoliveiralucas@contato.net" } > db.alunos.insert(aluno) MongoDB - InserirMongoDB - Inserir
  • 12. 16 de abril FLISOL 2016 12 > var query = { nome: "Lucas" } > db.alunos.find(query) MongoDB - ConsultarMongoDB - Consultar
  • 13. 16 de abril FLISOL 2016 13 OperadoresOperadores (Comparação)(Comparação) • $eq • $gt • $gte • $lt MongoDB - ConsultarMongoDB - Consultar
  • 14. 16 de abril FLISOL 2016 14 OperadoresOperadores (Comparação)(Comparação) • $lte • $ne • $in • $nin MongoDB - ConsultarMongoDB - Consultar
  • 15. 16 de abril FLISOL 2016 15 OperadoresOperadores (Lógico)(Lógico) • $or • $and • $not • $nor MongoDB - ConsultarMongoDB - Consultar
  • 16. 16 de abril FLISOL 2016 16 • Map-Reduce • Aggregation MongoDB - ConsultarMongoDB - Consultar
  • 17. 16 de abril FLISOL 2016 17 > var query = { nome: "Lucas" } > var mod = { nome: "Oliveira" } > db.alunos.update(query, mod) MongoDB - AtualizarMongoDB - Atualizar
  • 18. 16 de abril FLISOL 2016 18 Agora vamos fazer direito > var query = { nome: "de Oliveira" } > var mod = { $set: { nome: "deoliveiralucas" } } > db.alunos.update(query, mod) MongoDB - AtualizarMongoDB - Atualizar
  • 19. 16 de abril FLISOL 2016 19 Operadores (campo)Operadores (campo) • $set • $inc • $rename • $unset • $min • $max • $currentDate MongoDB - AtualizarMongoDB - Atualizar
  • 20. 16 de abril FLISOL 2016 20 Operadores (array)Operadores (array) • $addToSet • $pop • $pullAll • $pull • $push MongoDB - AtualizarMongoDB - Atualizar
  • 21. 16 de abril FLISOL 2016 21 > var cursor = db.alunos.find() > while(cursor.hasNext()){ print(tojson(cursor.next())) } MongoDB - CursorMongoDB - Cursor
  • 22. 16 de abril FLISOL 2016 22 for (var i = 0; i < 10000; i++) {for (var i = 0; i < 10000; i++) { var aluno = { nome: "nome" + i, sobrenome: "sobrenome" + i, idade: Math.floor((Math.random() * (80 - 15)) + 15) }; db.alunos.insert(aluno); } MongoDB - JavascriptMongoDB - Javascript
  • 23. 16 de abril FLISOL 2016 23 > mongod --replSet unisalReplica --port 27015 --dbpath /data/rs1> mongod --replSet unisalReplica --port 27015 --dbpath /data/rs1 > mongod --replSet unisalReplica --port 27016 --dbpath /data/rs2> mongod --replSet unisalReplica --port 27016 --dbpath /data/rs2 > mongod --replSet unisalReplica --port 27017 --dbpath /data/rs3> mongod --replSet unisalReplica --port 27017 --dbpath /data/rs3 > mongod --replSet unisalReplica --port 27018 --dbpath /data/arb> mongod --replSet unisalReplica --port 27018 --dbpath /data/arb MongoDB – Replica SetMongoDB – Replica Set
  • 24. 16 de abril FLISOL 2016 24 > mongo --port 27015> mongo --port 27015 > rs.initiate() > rs.status() > rs.add("deoliveiralucas:27016") > rs.add("deoliveiralucas:27017") > rs.addArb("deoliveiralucas:27018") MongoDB – Replica SetMongoDB – Replica Set
  • 25. 16 de abril FLISOL 2016 25 • MongoDB UniversityMongoDB University • Webschool.ioWebschool.io MongoDB – CursosMongoDB – Cursos
  • 26. 16 de abril FLISOL 2016 26 • https://docs.mongodb.org/manual/https://docs.mongodb.org/manual/ • https://docs.mongodb.org/manual/tutorial/query-documents/https://docs.mongodb.org/manual/tutorial/query-documents/ • https://docs.mongodb.org/manual/administration/replica-sets/https://docs.mongodb.org/manual/administration/replica-sets/ ReferênciasReferências
  • 27. 16 de abril FLISOL 2016 27 Obrigado!Obrigado!