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
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
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
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
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
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
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']