TeamStation AI System Report LATAM IT Salaries 2024
Â
Introduction to NoSQL db and mongoDB
1.
2. BIO
Senior SW Engineer
@RAI Radiotelevisione italiana
Rome, Italy
QUIT IT APP
@quititapp
GET IN TOUCH!
roberto.belardo@gmail.com
Â
Â
@robertobelardo
Â
Â
robertobelardo.wordpress.com
Â
Â
Skype:
 backslash451
Â
3. Traditional RDBMS
ACID (atomicity, Consistency, Isolation, Durability)
Great for relation data
widely adopted and well understood
TRANSACTIONS
JOINS
5. NOSQL DB
ONLY
â˘âŻ
â˘âŻ
â˘âŻ
â˘âŻ
ACID-LESS (NOT ALWAYS SWEET)
Elastic scaling
Big data
Flexible data models
KEY/VALUE STORES
memcached db
project voldemort
redis
dynamo
DOCUMENT DB
mongo db
couch base
GRAPH STORES
neo4j
orient db
allegro
virtuoso
wide column stores
big table
hbase
accumulo
cassandra
8. Jargon
RDBMS
Â
mongoDB
Â
Table
Â
Collec=on
Â
Row(s)
Â
(JSON)
 Document
Â
Column
Â
Field
Â
Index
Â
Index
Â
Join
Â
Embedding
 &
 Linking
Â
Par==on
Â
Shard
Â
GROUP
 BY
Â
Aggrega=on
Â
10. CRUD querying
INSERT
 a
 document
 in
 a
 collec7on
Â
> db.foo.insert({name:âSauronâ, type:âbad-assâ, address:âMordorâ, eyes:1})!
SELECT
 all
 documents
 in
 a
 collec7on
Â
> db.foo.find()!
SELECT
 some
 documents
 in
 a
 collec7on
Â
> db.foo.find({eyes:1})â¨
> db.foo.find({eyes:1, type:âbad-assâ})!
UPDATE
 a
 document
 in
 a
 collec7on
Â
> db.foo.save({_id:10, name:âSauronâ, type:âgood boyâ})!
> db.foo.update({type:âbad-assâ},{$inc:{eyes:1}},{multi:true})!
DELETE
 all
 documents
 in
 a
 collec7on
Â
> db.foo.remove()!
DELETE
 some
 documents
 in
 a
 collec7on
Â
> db.foo.remove({eyes:1})!
12. WRITE CONCERNS
âSpeciďŹes
 where
 a
 write
 (insert)
 opera3on
 has
 succeeded.â
Â
âIn
 some
 failure
 cases,
 write
 opera3ons
 issued
 with
 weak
 write
 concerns
 may
 not
 persist.â
Â
weak concern fast writes
âWith
 stronger
 write
 concerns,
 clients
 wait
 aAer
 sending
 a
 write
 opera3on
 for
 MongoDB
 to
Â
conďŹrm
 the
 write
 opera3on.â
Â
errors ignored w:-1
It
 does
 not
 acknowledge
 write
 op.
 and
 the
 client
 cannot
 detect
 failed
 write
 op.
Â
unacknowledged w:0
It
 does
 not
 acknowledge
 write
 op.
 but
 driver
 aUempt
 to
 handle
 network
 errors.
Â
acknowledged w:1
It
 conďŹrms
 the
 receipt
 of
 the
 write
 op.
 allowing
 clients
 to
 catch
 errors.
Â
journaled w:1,j:true
It
 conďŹrms
 the
 receipt
 of
 the
 write
 op.
 only
 aWer
 commiXng
 the
 data
 to
 journal.
Â
replica acknowledged w:n (n>1)
It
 conďŹrms
 the
 receipt
 of
 the
 write
 op.
Â
 aWer
 acknowledging
 primary
 and
 n-Ââ1
 secondaries.
Â
DEFAULT
Â
13. INDEXES
Without
 indexes,
 mongoDB
 must
 scan
 every
 document
 in
 a
 collec3on!
Â
index types
Default _ID
single field
compound index
multikey index
geospatial index
text indexes
hashed indexes
properties
unique indexes
sparse indexes
creating indexes
> db.foo.ensureIndex({phone-number:1}) !
!
> db.foo.ensureIndex({name:1, phone-number:1})!
!
> db.foo.ensureIndex({name:1}, {unique:true})!
> db.foo.ensureIndex({nickname:1}, {sparse:true})!
!
> db.foo.ensureIndex({a:1},{unique:true,sparse:true})!
15. SHARDING
horizontal scaling the mongoway
SHARDimmutable
KEYs
shard key is
{
range based sharding
hash based sharding
. easily divisible among shards
. high degree of randomness
. targets a single shard
. compound shard key
> db.runCommand({ shardcollection:âtest.usersâ, !
!
!
!
!
!key: { email:1}})!
split--ting
triggered automatically by a mongos if a chunk goes
beyond max size (default 64mb)
Balanc
ing
triggered automatically by a mongos if there is a chunk
imbalance (8+ chunks)