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

PDF
MongoDB Schema Design Tips & Tricks
ODP
MongoDB - Ekino PHP
PPTX
MongoDB Schema Design: Practical Applications and Implications
PPTX
Back to Basics Webinar 1: Introduction to NoSQL
PPTX
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
PPTX
High Performance Applications with MongoDB
PDF
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
PDF
MongoDB Europe 2016 - Big Data meets Big Compute
MongoDB Schema Design Tips & Tricks
MongoDB - Ekino PHP
MongoDB Schema Design: Practical Applications and Implications
Back to Basics Webinar 1: Introduction to NoSQL
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
High Performance Applications with MongoDB
MongoDB .local Munich 2019: Managing a Heterogeneous Stack with MongoDB & SQL
MongoDB Europe 2016 - Big Data meets Big Compute

What's hot (20)

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

Similar to MongoDB FabLab León (20)

PPTX
MongoDB Workshop Universidad de Huelva
PDF
MongoDB.pdf
PDF
2016 feb-23 pyugre-py_mongo
PDF
Using MongoDB and Python
PPTX
MongoDB_ppt.pptx
PDF
Mongo db eveningschemadesign
PPTX
Introduction to MongoDB
PPTX
How to learn MongoDB for beginner's
PDF
MongoDB Basics
PDF
MongoDB for Coder Training (Coding Serbia 2013)
KEY
Mongodb intro
PPTX
Introduction to MongoDB at IGDTUW
PPTX
MongoDB Evenings Minneapolis: MongoDB is Cool But When Should I Use It?
PDF
Quick overview on mongo db
PPTX
Introduction to MongoDB
PDF
Data as Documents: Overview and intro to MongoDB
PDF
Getting Ahead with MongoDB
PPT
9. Document Oriented Databases
PDF
MongoDB: a gentle, friendly overview
KEY
MongoDB Workshop Universidad de Huelva
MongoDB.pdf
2016 feb-23 pyugre-py_mongo
Using MongoDB and Python
MongoDB_ppt.pptx
Mongo db eveningschemadesign
Introduction to MongoDB
How to learn MongoDB for beginner's
MongoDB Basics
MongoDB for Coder Training (Coding Serbia 2013)
Mongodb intro
Introduction to MongoDB at IGDTUW
MongoDB Evenings Minneapolis: MongoDB is Cool But When Should I Use It?
Quick overview on mongo db
Introduction to MongoDB
Data as Documents: Overview and intro to MongoDB
Getting Ahead with MongoDB
9. Document Oriented Databases
MongoDB: a gentle, friendly overview
Ad

Recently uploaded (20)

PDF
.pdf is not working space design for the following data for the following dat...
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
Foundation of Data Science unit number two notes
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Computer network topology notes for revision
PPTX
Database Infoormation System (DBIS).pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Introduction to machine learning and Linear Models
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Mega Projects Data Mega Projects Data
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
annual-report-2024-2025 original latest.
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPT
Quality review (1)_presentation of this 21
PPTX
1_Introduction to advance data techniques.pptx
.pdf is not working space design for the following data for the following dat...
IB Computer Science - Internal Assessment.pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Foundation of Data Science unit number two notes
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Computer network topology notes for revision
Database Infoormation System (DBIS).pptx
Clinical guidelines as a resource for EBP(1).pdf
Introduction to machine learning and Linear Models
STUDY DESIGN details- Lt Col Maksud (21).pptx
Mega Projects Data Mega Projects Data
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
annual-report-2024-2025 original latest.
oil_refinery_comprehensive_20250804084928 (1).pptx
Quality review (1)_presentation of this 21
1_Introduction to advance data techniques.pptx

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: juanroycouto@gmail.com ❏ 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