SlideShare a Scribd company logo
basicsbasics
Learn all you need to know about MongoDB!
April 6, 2018
Who am I?
Juan Antonio Roy Couto
❏ MongoDB Master
❏ MongoDB DEV&DBA Certified
❏ MongoDB DBA at Grupo Undanet
❏ Email: juanroycouto@gmail.com
❏ Twitter: @juanroycouto
❏ Personal site: http://www.juanroy.es
2
❏ Advantages
❏ Concepts
❏ Products
❏ Characteristics
❏ Schema Design
❏ Data Modelling
❏ Installation Types
❏ Must Know
❏ CRUD
Agenda MongoDB Overview
3
❏ Aggregation Framework
❏ Analytics Tools
❏ Indexes
❏ Replica Set
❏ Sharded Cluster
❏ Scaling
❏ Security
❏ Python Driver Overview
❏ Resources
Advantages MongoDB Overview
❏ High Availability
❏ Data Safety
❏ Automatic Failover
❏ Scalability
4
❏ Faster development
❏ Real time analytics
❏ Better strategic decisions
❏ Reduce costs and time to
market
5
MongoDB SQL
Database Database
Collection Table
Document Row
Embedding/$lookup Join
Concepts MongoDB Overview
Products
https://www.mongodb.com/products/overview
MongoDB Overview
6
❏ Atlas (Database as a Service)
❏ Stitch (Backend as a Service)
❏ Drivers
❏ Ops & Cloud Manager
❏ Compass
❏ Hadoop & Spark connector
❏ BI connector
Characteristics MongoDB Overview
7
❏ Open Source General Purpose NoSQL
Database
❏ Document Oriented
❏ Non-Structured Data
❏ Schemaless
❏ Security (Authentication & Authorization)
❏ Schema Validation
❏ Backup & Restore
❏ Automation
❏ Encryption
❏ Geo-Location
❏ Graph processing
❏ Aggregation Framework
❏ Joins
❏ Change Streams
❏ Retryable Writes
❏ Pluggable Storage Engine API
❏ Multi-document ACID transactions on
June, 4.0 version
SQL Schema Design MongoDB Overview
8
❏ Customer Key
❏ First Name
❏ Last Name
Tables
Customers
❏ Address Key
❏ Customer Key
❏ Street
❏ Number
❏ Location
Addresses
❏ Pet Key
❏ Customer Key
❏ Type
❏ Breed
❏ Name
Pets
MongoDB Schema Design MongoDB Overview
9
Customers Collection
❏ Street
❏ Number
❏ Location
Addresses
❏ Type
❏ Breed
❏ Name
Pets
Customers Info
❏ First Name
❏ Last Name
❏ Type
❏ Breed
❏ Name
JSON Document MongoDB Overview
> db.customers.findOne()
{
"_id" : ObjectId("54131863041cd2e6181156ba"),
"date" : ISODate("2018-02-28T23:12:25Z"),
"first_name" : "Peter",
"last_name" : "Keil",
"address" : {
"street" : "C/Alcalá",
"number" : 123,
"location" : "Madrid",
},
"pets" : [
{
"type" : "Dog",
"breed" : "Airedale Terrier",
"name" : "Linda",
"location" : {
type : "Point",
coordinates : [ -5.724332, 40.959219 ]
}
},
{
"type" : "Dog",
...
}
]
}
>
10
_id
string
date
subdocument
number
array
geo-location
Data Modelling MongoDB Overview
11
1:1 Employee-Resume
❏ Access frequency
❏ Documents size
❏ Data atomicity
1:N City-Citizen
❏ Two linked collections
from N to 1
N:N Books-Authors
❏ Two collections linked via
array
1:Few Post-Comments
❏ One collection with
embedded data
Limits: 16MB/doc
Relational Approach vs Document Model MongoDB Overview
12
Relational Approach MongoDB Document Model
{
"_id" : ObjectId("54131863041cd2e6181156ba"),
"date" : ISODate("2018-02-28T23:12:25Z"),
"first_name" : "Peter",
"last_name" : "Keil",
"address" : {
"street" : "C/Alcalá",
"number" : 123,
"location" : "Madrid",
},
"pets" : [
{
"type" : "Dog",
"breed" : "Airedale Terrier",
"name" : "Linda",
"location" : {
type : "Point",
coordinates : [ -5.724332, 40.959219 ]
}
},
{
"type" : "Dog",
...
}
]
}
Installation Types - Standalone MongoDB Overview
13
MongoDB
Client
DRIVER
Client
DRIVER
Client
DRIVER
Installation Types - Replica Set MongoDB Overview
14
SecondarySecondary
Primary
Client
DRIVER
Client
DRIVER
Client
DRIVER
Replica Set
Installation Types - Sharded Cluster MongoDB Overview
15
Replica Set
Secondary
Secondary
Primary
Client
DRIVER
Client
DRIVER
Client
DRIVER
Secondary
Secondary
Primary
Secondary
Secondary
Primary
Secondary
Secondary
Primary
mongos mongos mongos
config server
config server
config server
Shard 0 Shard 1 Shard 2 Shard N-1
❏ In MongodB it’s not necessary to:
❏ Create a Database, simply use it!
❏ Create a Collection, simply insert one document on it!
❏ Predefine the schema of the collections, MongoDB it’s
schemaless
❏ Once MongoDB is running on your machine connect to your
database from the shell in this way:
$ mongo
Must Know MongoDB Overview
16
❏ Find
❏ Insert
❏ Bulk inserts for massive Data Load
❏ Update
❏ Remove
CRUD MongoDB Overview
17
# Select database
> use usalfullstack
# Select all documents on the collection
> db.<collectionName>.find().pretty()
# Select documents that match the criteria
> criteria = { <field1> : <value1>, <field2> : <value2> }
> db.<collectionName>.find( criteria)
# Filtering fields
> projection = { <field1> : 1, <field2> : 1 }
> db.<collectionName>.find({}, projection)
CRUD - Find MongoDB Overview
18
# Inserting a document with fields, arrays and subdocuments
> doc =
{ <field1> : <value1>,
<field2> : [
{ <field3> : <value3>, <field4> : <value4>, <field5> : <value5>
},
{ <field6> : <value6>, <field7> : <value7> },
{ <field8> : <value8> }
]
}
> db.<collectionName>.insert( doc)
CRUD - Insert MongoDB Overview
19
# Updating the document that match the criteria
> criteria = { <field1> : <value1 }
> new_doc = { <field2> : <value2>, <field3> : <value3> }
> db.<collectionName>.update( criteria, new_doc)
# Updating only one field of all the documents that match the criteria
> criteria = { <field1> : <value1 }
> new_value = { operator : { <field3> : <value3> } }
> db.<collectionName>.update( criteria, new_value, { multi : true })
CRUD - Update MongoDB Overview
20
CRUD - Remove MongoDB Overview
# Removing all documents that match the criteria
> criteria = { <field1> : <value1 }
> db.<collectionName>.remove( criteria)
21
Data Analytics with the
Aggregation Framework
MongoDB Overview
22
Aggregation Framework - Pipeline MongoDB Overview
23
> criteria = { $match : { <field1> : <value1> } }
> group = { $group : { "$_id" : "$<field2>",
<new_field> : { <operator> : "$<field3>" } } }
> projection = { $project : { "_id" : 0,
<new_field2>: "$_id",
<new_field3>: "$<new_field>"
}
}
> sort = { $sort : { <new_field3> : 1 } }
> pipeline = [ criteria, group, projection, sort ]
> db.<collectionName>.aggregate(pipeline)
Data analytics Tools MongoDB Overview
24
❏ Internals
❏ Aggregation Framework
❏ Map Reduce
❏ Externals
❏ Spark
❏ Hadoop
❏ Tableau (BI)
❏ ...
MongoDB Overview
25
Indexing - Types
❏ _id
❏ Single
❏ Compound
❏ Multikey
❏ Full Text
❏ GeoSpatial
❏ Hashed
MongoDB Overview
26
Indexing - Properties
❏ Unique
❏ Sparse
❏ TTL
❏ Partial
MongoDB Overview
27
Indexing - Improving Your Queries
.explain()
❏ queryPlanner
❏ executionStats
❏ allPlansExecution
Replica Set
❏ High Availability
❏ Data Safety
❏ Automatic Node Recovery
❏ Read Preference
❏ Write Concern
Replica Set
Secondary
Secondary
Primary
MongoDB Overview
28
❏ Scale out
❏ Even data distribution across all of the
shards based on a shard key
❏ A shard key range belongs to only one
shard
❏ More efficient queries (performance)
Sharded Cluster
Cluster
Shard 0 Shard 2Shard 1
A-I J-Q R-Z
MongoDB Overview
29
Sharded Cluster - Config Servers
❏ config database
❏ Metadata:
❏ Cluster shards list
❏ Data per shard (chunk ranges)
❏ ...
❏ Replica Set
MongoDB Overview
30
Replica Set
config server
config server
config server
❏ Receives client requests and returns
results.
❏ Reads the metadata and sends the
query to the necessary shard/shards.
❏ Does not store data.
❏ Keeps a cache version of the
metadata.
Sharded Cluster - mongos MongoDB Overview
31
Replica Set
DRIVER
Secondary
Secondary
Primary
Secondary
Secondary
Primary
mongos
config
server
config server
config server
Shard 0 Shard N-1
How To Scale Your App - Shard Key MongoDB Overview
32
❏ Monotonically Increasing
❏ Easy divisible❏ Randomness❏ Cardinality
How To Scale Your App
Sharding a Collection
MongoDB Overview
Shard 0 Shard 1 Shard 2 Shard 3
mongos
Client
Migrations
How To Scale Your App - Pre-Splitting MongoDB Overview
34
● Useful for storing data
directly in the shards
(massive data loads).
● Avoid bottlenecks.
● MongoDB does not need to
split or migrate chunks.
● After the split, the migration
must be finished before
data loading.
Cluster
Shard 0 Shard 2Shard 1
Chunk 1
Chunk 5
Chunk 3
Chunk 4
Chunk 2
How To Scale Your App
Tag-Aware Sharding
MongoDB Overview
35
● Tags are used when you want to pin ranges to a specific shard.
shard0
EMEA
shard1
APAC
shard2
LATAM
shard3
NORAM
Security MongoDB Overview
36
● Authentication
○ Users
○ Servers
● Authorization
○ Roles
○ Privileges (actions
over resources)
● Read-Only Views
● Encryption
● Auditing
Python Driver - CRUD MongoDB Overview
37
PyMongo Server
Finding
find find
find_one findOne
Inserting
insert_one insert
insert_many bulk
Updating
update_one update
update_many update
replace_one update
Deleting
delete_one remove
delete_many remove
Python Driver - CRUD Examples MongoDB Overview
38
$python find_one.py
El nombre de la persona leida es: Peter
find_one.py
import pymongo
from pymongo import MongoClient
try:
connMDB = MongoClient('localhost', 27017)
except Exception as e:
print 'Error de conexion a la base de datos', type(e),
e
db = connMDB.usalfullstack
personas = db.personas
persona = personas. find_one()
print 'El nombre de la persona leida es: ', persona['name']
Python Driver - CRUD Examples MongoDB Overview
39
Insert
pedro = { 'firstname':'Pedro', 'lastname':'García' }
maria = { 'firstname':'María', 'lastname':'Pérez' }
doc = [ pedro, maria ]
customers.insert_many(doc, ordered:True)
Update
customers.update_one({'_id':customer_id},
{$set:{'city':'Huelva'}})
Remove
customers.delete_one( { '_id' : customer_id } )
Python Driver - Cursors And Exceptions MongoDB Overview
40
import pymongo
import sys
from pymongo import MongoClient
connection = MongoClient('localhost',27017)
db = connection.test
customers = db.customers
query = { 'firstname' : 'Juan' }
projection = { 'city' : 1, '_id' : 0 }
try:
cursor = customers.find(query,projection)
except Exception as e:
print 'Unexpected error: ', type(e), e
for doc in cursor:
print doc['city']
Resources
41
◆ Official MongoDB Documentation
● https://docs.mongodb.org/manual/
◆ pymongo
● https://pypi.python.org/pypi/pymongo/3.6.0
◆ Install MongoDB on Linux
● https://docs.mongodb.com/manual/administration/install-on-linux/
◆ Install MongoDB on macOS
● https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
◆ Install MongoDB on Windows
● https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
◆ MongoDB University
● https://university.mongodb.com/
MongoDB Overview
Questions?
Questions?
42
MongoDB Overview
basicsbasics
Thank you for your attention!
basicsbasics
Learn all you need to know about MongoDB!
April 6, 2018

More Related Content

What's hot (20)

PPTX
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
PPTX
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
MongoDB
 
PDF
MongoDB Basics Unileon
Juan Antonio Roy Couto
 
PDF
Advanced Schema Design Patterns
MongoDB
 
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
PPTX
MongoDB 101
Abhijeet Vaikar
 
PPTX
Mongo db – document oriented database
Wojciech Sznapka
 
PPTX
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
PPTX
MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101
MongoDB
 
PPTX
Conceptos básicos. Seminario web 5: Introducción a Aggregation Framework
MongoDB
 
PPTX
MongoDB + Spring
Norberto Leite
 
PPTX
Webinar: Best Practices for Getting Started with MongoDB
MongoDB
 
PPTX
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
Gianfranco Palumbo
 
PPTX
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
PPTX
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
PPTX
Back to Basics Webinar 1: Introduction to NoSQL
MongoDB
 
PPTX
Basics of MongoDB
HabileLabs
 
PPTX
Back to Basics, webinar 2: La tua prima applicazione MongoDB
MongoDB
 
PPTX
Mongo db operations_v2
Thanabalan Sathneeganandan
 
PDF
How To Connect Spark To Your Own Datasource
MongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
MongoDB
 
MongoDB Basics Unileon
Juan Antonio Roy Couto
 
Advanced Schema Design Patterns
MongoDB
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
MongoDB 101
Abhijeet Vaikar
 
Mongo db – document oriented database
Wojciech Sznapka
 
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101
MongoDB
 
Conceptos básicos. Seminario web 5: Introducción a Aggregation Framework
MongoDB
 
MongoDB + Spring
Norberto Leite
 
Webinar: Best Practices for Getting Started with MongoDB
MongoDB
 
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
Gianfranco Palumbo
 
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
Back to Basics Webinar 1: Introduction to NoSQL
MongoDB
 
Basics of MongoDB
HabileLabs
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
MongoDB
 
Mongo db operations_v2
Thanabalan Sathneeganandan
 
How To Connect Spark To Your Own Datasource
MongoDB
 

Similar to MongoDB FabLab León (20)

PPTX
MongoDB Workshop Universidad de Huelva
Juan Antonio Roy Couto
 
PDF
Spark & Cassandra - DevFest Córdoba
Jose Mº Muñoz
 
KEY
Mongodb intro
christkv
 
PDF
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
PPTX
Introduction To MongoDB
ElieHannouch
 
PPTX
Eagle6 mongo dc revised
MongoDB
 
PPTX
Eagle6 Enterprise Situational Awareness
MongoDB
 
PPTX
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
PPT
MongoDB Tick Data Presentation
MongoDB
 
PDF
Using MongoDB and Python
Mike Bright
 
PDF
2016 feb-23 pyugre-py_mongo
Michael Bright
 
PDF
MongoDB
wiTTyMinds1
 
PDF
Building your first app with MongoDB
Norberto Leite
 
PPTX
Data stores: beyond relational databases
Javier García Magna
 
PDF
Real-time analytics with Druid at Appsflyer
Michael Spector
 
PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
PPTX
Joins and Other MongoDB 3.2 Aggregation Enhancements
Andrew Morgan
 
PDF
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA
 
PDF
RMLL 2013 - Synchronize OpenLDAP and Active Directory with LSC
Clément OUDOT
 
PPTX
How sitecore depends on mongo db for scalability and performance, and what it...
Antonios Giannopoulos
 
MongoDB Workshop Universidad de Huelva
Juan Antonio Roy Couto
 
Spark & Cassandra - DevFest Córdoba
Jose Mº Muñoz
 
Mongodb intro
christkv
 
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
Introduction To MongoDB
ElieHannouch
 
Eagle6 mongo dc revised
MongoDB
 
Eagle6 Enterprise Situational Awareness
MongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
MongoDB Tick Data Presentation
MongoDB
 
Using MongoDB and Python
Mike Bright
 
2016 feb-23 pyugre-py_mongo
Michael Bright
 
MongoDB
wiTTyMinds1
 
Building your first app with MongoDB
Norberto Leite
 
Data stores: beyond relational databases
Javier García Magna
 
Real-time analytics with Druid at Appsflyer
Michael Spector
 
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
Joins and Other MongoDB 3.2 Aggregation Enhancements
Andrew Morgan
 
Data Con LA 2022 - What's new with MongoDB 6.0 and Atlas
Data Con LA
 
RMLL 2013 - Synchronize OpenLDAP and Active Directory with LSC
Clément OUDOT
 
How sitecore depends on mongo db for scalability and performance, and what it...
Antonios Giannopoulos
 
Ad

Recently uploaded (20)

PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PDF
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PPTX
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
PDF
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PDF
Early_Diabetes_Detection_using_Machine_L.pdf
maria879693
 
PPTX
Climate Action.pptx action plan for climate
justfortalabat
 
PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PPTX
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
Early_Diabetes_Detection_using_Machine_L.pdf
maria879693
 
Climate Action.pptx action plan for climate
justfortalabat
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
Ad

MongoDB FabLab León

  • 1. basicsbasics Learn all you need to know about MongoDB! April 6, 2018
  • 2. Who am I? Juan Antonio Roy Couto ❏ MongoDB Master ❏ MongoDB DEV&DBA Certified ❏ MongoDB DBA at Grupo Undanet ❏ Email: [email protected] ❏ Twitter: @juanroycouto ❏ Personal site: http://www.juanroy.es 2
  • 3. ❏ Advantages ❏ Concepts ❏ Products ❏ Characteristics ❏ Schema Design ❏ Data Modelling ❏ Installation Types ❏ Must Know ❏ CRUD Agenda MongoDB Overview 3 ❏ Aggregation Framework ❏ Analytics Tools ❏ Indexes ❏ Replica Set ❏ Sharded Cluster ❏ Scaling ❏ Security ❏ Python Driver Overview ❏ Resources
  • 4. Advantages MongoDB Overview ❏ High Availability ❏ Data Safety ❏ Automatic Failover ❏ Scalability 4 ❏ Faster development ❏ Real time analytics ❏ Better strategic decisions ❏ Reduce costs and time to market
  • 5. 5 MongoDB SQL Database Database Collection Table Document Row Embedding/$lookup Join Concepts MongoDB Overview
  • 6. Products https://www.mongodb.com/products/overview MongoDB Overview 6 ❏ Atlas (Database as a Service) ❏ Stitch (Backend as a Service) ❏ Drivers ❏ Ops & Cloud Manager ❏ Compass ❏ Hadoop & Spark connector ❏ BI connector
  • 7. Characteristics MongoDB Overview 7 ❏ Open Source General Purpose NoSQL Database ❏ Document Oriented ❏ Non-Structured Data ❏ Schemaless ❏ Security (Authentication & Authorization) ❏ Schema Validation ❏ Backup & Restore ❏ Automation ❏ Encryption ❏ Geo-Location ❏ Graph processing ❏ Aggregation Framework ❏ Joins ❏ Change Streams ❏ Retryable Writes ❏ Pluggable Storage Engine API ❏ Multi-document ACID transactions on June, 4.0 version
  • 8. SQL Schema Design MongoDB Overview 8 ❏ Customer Key ❏ First Name ❏ Last Name Tables Customers ❏ Address Key ❏ Customer Key ❏ Street ❏ Number ❏ Location Addresses ❏ Pet Key ❏ Customer Key ❏ Type ❏ Breed ❏ Name Pets
  • 9. MongoDB Schema Design MongoDB Overview 9 Customers Collection ❏ Street ❏ Number ❏ Location Addresses ❏ Type ❏ Breed ❏ Name Pets Customers Info ❏ First Name ❏ Last Name ❏ Type ❏ Breed ❏ Name
  • 10. JSON Document MongoDB Overview > db.customers.findOne() { "_id" : ObjectId("54131863041cd2e6181156ba"), "date" : ISODate("2018-02-28T23:12:25Z"), "first_name" : "Peter", "last_name" : "Keil", "address" : { "street" : "C/Alcalá", "number" : 123, "location" : "Madrid", }, "pets" : [ { "type" : "Dog", "breed" : "Airedale Terrier", "name" : "Linda", "location" : { type : "Point", coordinates : [ -5.724332, 40.959219 ] } }, { "type" : "Dog", ... } ] } > 10 _id string date subdocument number array geo-location
  • 11. Data Modelling MongoDB Overview 11 1:1 Employee-Resume ❏ Access frequency ❏ Documents size ❏ Data atomicity 1:N City-Citizen ❏ Two linked collections from N to 1 N:N Books-Authors ❏ Two collections linked via array 1:Few Post-Comments ❏ One collection with embedded data Limits: 16MB/doc
  • 12. Relational Approach vs Document Model MongoDB Overview 12 Relational Approach MongoDB Document Model { "_id" : ObjectId("54131863041cd2e6181156ba"), "date" : ISODate("2018-02-28T23:12:25Z"), "first_name" : "Peter", "last_name" : "Keil", "address" : { "street" : "C/Alcalá", "number" : 123, "location" : "Madrid", }, "pets" : [ { "type" : "Dog", "breed" : "Airedale Terrier", "name" : "Linda", "location" : { type : "Point", coordinates : [ -5.724332, 40.959219 ] } }, { "type" : "Dog", ... } ] }
  • 13. Installation Types - Standalone MongoDB Overview 13 MongoDB Client DRIVER Client DRIVER Client DRIVER
  • 14. Installation Types - Replica Set MongoDB Overview 14 SecondarySecondary Primary Client DRIVER Client DRIVER Client DRIVER Replica Set
  • 15. Installation Types - Sharded Cluster MongoDB Overview 15 Replica Set Secondary Secondary Primary Client DRIVER Client DRIVER Client DRIVER Secondary Secondary Primary Secondary Secondary Primary Secondary Secondary Primary mongos mongos mongos config server config server config server Shard 0 Shard 1 Shard 2 Shard N-1
  • 16. ❏ In MongodB it’s not necessary to: ❏ Create a Database, simply use it! ❏ Create a Collection, simply insert one document on it! ❏ Predefine the schema of the collections, MongoDB it’s schemaless ❏ Once MongoDB is running on your machine connect to your database from the shell in this way: $ mongo Must Know MongoDB Overview 16
  • 17. ❏ Find ❏ Insert ❏ Bulk inserts for massive Data Load ❏ Update ❏ Remove CRUD MongoDB Overview 17
  • 18. # Select database > use usalfullstack # Select all documents on the collection > db.<collectionName>.find().pretty() # Select documents that match the criteria > criteria = { <field1> : <value1>, <field2> : <value2> } > db.<collectionName>.find( criteria) # Filtering fields > projection = { <field1> : 1, <field2> : 1 } > db.<collectionName>.find({}, projection) CRUD - Find MongoDB Overview 18
  • 19. # Inserting a document with fields, arrays and subdocuments > doc = { <field1> : <value1>, <field2> : [ { <field3> : <value3>, <field4> : <value4>, <field5> : <value5> }, { <field6> : <value6>, <field7> : <value7> }, { <field8> : <value8> } ] } > db.<collectionName>.insert( doc) CRUD - Insert MongoDB Overview 19
  • 20. # Updating the document that match the criteria > criteria = { <field1> : <value1 } > new_doc = { <field2> : <value2>, <field3> : <value3> } > db.<collectionName>.update( criteria, new_doc) # Updating only one field of all the documents that match the criteria > criteria = { <field1> : <value1 } > new_value = { operator : { <field3> : <value3> } } > db.<collectionName>.update( criteria, new_value, { multi : true }) CRUD - Update MongoDB Overview 20
  • 21. CRUD - Remove MongoDB Overview # Removing all documents that match the criteria > criteria = { <field1> : <value1 } > db.<collectionName>.remove( criteria) 21
  • 22. Data Analytics with the Aggregation Framework MongoDB Overview 22
  • 23. Aggregation Framework - Pipeline MongoDB Overview 23 > criteria = { $match : { <field1> : <value1> } } > group = { $group : { "$_id" : "$<field2>", <new_field> : { <operator> : "$<field3>" } } } > projection = { $project : { "_id" : 0, <new_field2>: "$_id", <new_field3>: "$<new_field>" } } > sort = { $sort : { <new_field3> : 1 } } > pipeline = [ criteria, group, projection, sort ] > db.<collectionName>.aggregate(pipeline)
  • 24. Data analytics Tools MongoDB Overview 24 ❏ Internals ❏ Aggregation Framework ❏ Map Reduce ❏ Externals ❏ Spark ❏ Hadoop ❏ Tableau (BI) ❏ ...
  • 25. MongoDB Overview 25 Indexing - Types ❏ _id ❏ Single ❏ Compound ❏ Multikey ❏ Full Text ❏ GeoSpatial ❏ Hashed
  • 26. MongoDB Overview 26 Indexing - Properties ❏ Unique ❏ Sparse ❏ TTL ❏ Partial
  • 27. MongoDB Overview 27 Indexing - Improving Your Queries .explain() ❏ queryPlanner ❏ executionStats ❏ allPlansExecution
  • 28. Replica Set ❏ High Availability ❏ Data Safety ❏ Automatic Node Recovery ❏ Read Preference ❏ Write Concern Replica Set Secondary Secondary Primary MongoDB Overview 28
  • 29. ❏ Scale out ❏ Even data distribution across all of the shards based on a shard key ❏ A shard key range belongs to only one shard ❏ More efficient queries (performance) Sharded Cluster Cluster Shard 0 Shard 2Shard 1 A-I J-Q R-Z MongoDB Overview 29
  • 30. Sharded Cluster - Config Servers ❏ config database ❏ Metadata: ❏ Cluster shards list ❏ Data per shard (chunk ranges) ❏ ... ❏ Replica Set MongoDB Overview 30 Replica Set config server config server config server
  • 31. ❏ Receives client requests and returns results. ❏ Reads the metadata and sends the query to the necessary shard/shards. ❏ Does not store data. ❏ Keeps a cache version of the metadata. Sharded Cluster - mongos MongoDB Overview 31 Replica Set DRIVER Secondary Secondary Primary Secondary Secondary Primary mongos config server config server config server Shard 0 Shard N-1
  • 32. How To Scale Your App - Shard Key MongoDB Overview 32 ❏ Monotonically Increasing ❏ Easy divisible❏ Randomness❏ Cardinality
  • 33. How To Scale Your App Sharding a Collection MongoDB Overview Shard 0 Shard 1 Shard 2 Shard 3 mongos Client Migrations
  • 34. How To Scale Your App - Pre-Splitting MongoDB Overview 34 ● Useful for storing data directly in the shards (massive data loads). ● Avoid bottlenecks. ● MongoDB does not need to split or migrate chunks. ● After the split, the migration must be finished before data loading. Cluster Shard 0 Shard 2Shard 1 Chunk 1 Chunk 5 Chunk 3 Chunk 4 Chunk 2
  • 35. How To Scale Your App Tag-Aware Sharding MongoDB Overview 35 ● Tags are used when you want to pin ranges to a specific shard. shard0 EMEA shard1 APAC shard2 LATAM shard3 NORAM
  • 36. Security MongoDB Overview 36 ● Authentication ○ Users ○ Servers ● Authorization ○ Roles ○ Privileges (actions over resources) ● Read-Only Views ● Encryption ● Auditing
  • 37. Python Driver - CRUD MongoDB Overview 37 PyMongo Server Finding find find find_one findOne Inserting insert_one insert insert_many bulk Updating update_one update update_many update replace_one update Deleting delete_one remove delete_many remove
  • 38. Python Driver - CRUD Examples MongoDB Overview 38 $python find_one.py El nombre de la persona leida es: Peter find_one.py import pymongo from pymongo import MongoClient try: connMDB = MongoClient('localhost', 27017) except Exception as e: print 'Error de conexion a la base de datos', type(e), e db = connMDB.usalfullstack personas = db.personas persona = personas. find_one() print 'El nombre de la persona leida es: ', persona['name']
  • 39. Python Driver - CRUD Examples MongoDB Overview 39 Insert pedro = { 'firstname':'Pedro', 'lastname':'García' } maria = { 'firstname':'María', 'lastname':'Pérez' } doc = [ pedro, maria ] customers.insert_many(doc, ordered:True) Update customers.update_one({'_id':customer_id}, {$set:{'city':'Huelva'}}) Remove customers.delete_one( { '_id' : customer_id } )
  • 40. Python Driver - Cursors And Exceptions MongoDB Overview 40 import pymongo import sys from pymongo import MongoClient connection = MongoClient('localhost',27017) db = connection.test customers = db.customers query = { 'firstname' : 'Juan' } projection = { 'city' : 1, '_id' : 0 } try: cursor = customers.find(query,projection) except Exception as e: print 'Unexpected error: ', type(e), e for doc in cursor: print doc['city']
  • 41. Resources 41 ◆ Official MongoDB Documentation ● https://docs.mongodb.org/manual/ ◆ pymongo ● https://pypi.python.org/pypi/pymongo/3.6.0 ◆ Install MongoDB on Linux ● https://docs.mongodb.com/manual/administration/install-on-linux/ ◆ Install MongoDB on macOS ● https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ ◆ Install MongoDB on Windows ● https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ ◆ MongoDB University ● https://university.mongodb.com/ MongoDB Overview
  • 43. basicsbasics Thank you for your attention! basicsbasics Learn all you need to know about MongoDB! April 6, 2018