{

}

name : ‘Marcelo Cenerino’,
company: ‘Amil’,
date : ‘2013-10-30T08:30:00.000Z’
What is MongoDB?
Here’s a definition:
MongoDB (from humongous) is an open-source, highperformance, scalable, general purpose database. It i...
You can have at most two of these properties for
any shared-data system. Dr. Eric A. Brewer, 2000
Main characteristics
• Document based

• Schemaless
• Open source (on GitHub)

• High performance
• Horizontally scalable
...
Customers

eBay, Ericsson, EA, SAP, Telefonica, Code School, Abril...
MongoDB vs. RDBMS
RDBMS: data is structured as tables
id

nome

idade

genero

1

João

25

Masculino

2

Maria

30

Feminino

3

Pedro

40
...
Document oriented???
Document oriented
{
_id : 1,
nome : 'João',
idade : 25,
genero : 'Masculino'
}
MongoDB stores data as document in a
binary...
RDBMS

vs.

MongoDB

Table

Collection

Row

Document

Column

Field

Index

Index

Joins

Embedded doc.

FK

Reference

P...
Transaction Model

MongoDB guarantees atomic updates
to data at the document level.
Relational schema design
Relational schema design
• Large ERD diagrams

• Create table statements
• ORM to map tables to objects
• Tables just to j...
In a MongoDB based app we

start building our app
and let the schema evolve.
Mongo “schema” design
Article

User
name
email

title
date
text
author

Comment[]
author
date
text

Tag[]
value
Getting started with
MongoDB
> mongod

> mongo
Basic CRUD operations
Inserting a document
> user = {name : ‘marcelo’, age : 29, gender : ‘Male’}
> db.users.insert(user)
>

• No collection cre...
Querying a document
> db.users.findOne()
{
"_id" : ObjectId("5269d66271de67aa7c3c41b4"),
"name" : “marcelo",
"age" : 29,
“...
Querying a document
> db.users.find({name : 'maria', age : {$gt : 25}})
{ "_id" : ObjectId("526f1af1dac0a62cdc152a96"), "n...
Operators
Group

Operators

Comparison

$gt, $gte, $in, $lt, $lte, $ne, $nin

Logical

$or, $and, $not, $nor

Element

$ex...
Updating a document
> db.users.find({age : {$gt : 25}}, {_id : 0})
{ "name" : "maria", "age" : 26 }
{ "name" : "marcelo", ...
Removing a document
> db.users.remove({name : 'maria'})
> db.users.find().pretty()
{
"_id" : ObjectId("526f1cb3dac0a62cdc1...
Indexing
Querying a large collection without index
> db.estabelecimentos.count()
307929
>
> db.estabelecimentos.find({'localizacao....
Showing collection’s indexes
> db.estabelecimentos.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mapa-servicos....
Creating an index
> // creating an index
> db.estabelecimentos.ensureIndex({'localizacao.cidade' : 1})
> db.estabeleciment...
Same query, now using index
> db.estabelecimentos.find({'localizacao.cidade' : 'PIACATU'}).explain()
{
"cursor" : "BtreeCu...
Geospatial index
> db.estabelecimentos.ensureIndex({'localizacao.coordenadas' : '2dsphere'})
> db.estabelecimentos.getInde...
Geospatial index
> lng = -46.80208830000004
> lat = -23.515985699999998
> distance = 30 / 6378.137
>
> db.estabelecimentos...
Aggregation Framework
Aggregation Framework

Pipeline Operators: $project, $match, $limit, $skip, $unwind, $group, $sort, $geoNear
Aggregation Framework
> db.estabelecimentos.aggregate([{$match : {'localizacao.uf' : 'SP'}}, {$group : {_id :
'$localizaca...
Mongo Driver for Java
Spring Data MongoDB
http://projects.spring.io/spring-data-mongodb/
Replica Sets
Replica Sets
• A replica set is a group of mongod instances that host the
same data set
• Replication provides redundancy ...
Replica Sets
Replica Sets
Sharding
Sharding
Issues of scaling:
• High query rates can exhaust the CPU capacity of the server
• Larger data sets exceed the st...
Sharding

Horizontally Scalable

• Sharding is the process of storing data across multiple machines
• Each shard is an ind...
Sharded clusters
Range Based Sharding

• Supports more efficient range queries
• Results in an uneven distribution of data
• Monotonically ...
Hash Based Sharding

Ensures an even distribution of data at the expense of efficient range queries
https://education.mongodb.com/
Books
db.audience.find({‘question’ : true})
Thanks everyone!
Hope you’ve enjoyed it.
Talk MongoDB - Amil
Upcoming SlideShare
Loading in...5
×

Talk MongoDB - Amil

426

Published on

Overview das principais features do MongoDB.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
426
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Talk MongoDB - Amil"

  1. 1. { } name : ‘Marcelo Cenerino’, company: ‘Amil’, date : ‘2013-10-30T08:30:00.000Z’
  2. 2. What is MongoDB?
  3. 3. Here’s a definition: MongoDB (from humongous) is an open-source, highperformance, scalable, general purpose database. It is used by organizations of all sizes to power online applications where low latency and high availability are critical requirements of the system.
  4. 4. You can have at most two of these properties for any shared-data system. Dr. Eric A. Brewer, 2000
  5. 5. Main characteristics • Document based • Schemaless • Open source (on GitHub) • High performance • Horizontally scalable • Full featured
  6. 6. Customers eBay, Ericsson, EA, SAP, Telefonica, Code School, Abril...
  7. 7. MongoDB vs. RDBMS
  8. 8. RDBMS: data is structured as tables id nome idade genero 1 João 25 Masculino 2 Maria 30 Feminino 3 Pedro 40 Masculino ... ... ...
  9. 9. Document oriented???
  10. 10. Document oriented { _id : 1, nome : 'João', idade : 25, genero : 'Masculino' } MongoDB stores data as document in a binary representation called BSON (Binary JSON) Size: up to 16 MB
  11. 11. RDBMS vs. MongoDB Table Collection Row Document Column Field Index Index Joins Embedded doc. FK Reference Partition Shard
  12. 12. Transaction Model MongoDB guarantees atomic updates to data at the document level.
  13. 13. Relational schema design
  14. 14. Relational schema design • Large ERD diagrams • Create table statements • ORM to map tables to objects • Tables just to join tables together • Lots of revision and alter table statements until we get it just right
  15. 15. In a MongoDB based app we start building our app and let the schema evolve.
  16. 16. Mongo “schema” design Article User name email title date text author Comment[] author date text Tag[] value
  17. 17. Getting started with MongoDB
  18. 18. > mongod > mongo
  19. 19. Basic CRUD operations
  20. 20. Inserting a document > user = {name : ‘marcelo’, age : 29, gender : ‘Male’} > db.users.insert(user) > • No collection creation needed!
  21. 21. Querying a document > db.users.findOne() { "_id" : ObjectId("5269d66271de67aa7c3c41b4"), "name" : “marcelo", "age" : 29, “gender" : “male" } • • • • _id is the primary key in MongoDB Automatically indexed Automatically created as an ObjectId if not provided Any unique immutable value could be used
  22. 22. Querying a document > db.users.find({name : 'maria', age : {$gt : 25}}) { "_id" : ObjectId("526f1af1dac0a62cdc152a96"), "name" : "maria", "age" : 26 }
  23. 23. Operators Group Operators Comparison $gt, $gte, $in, $lt, $lte, $ne, $nin Logical $or, $and, $not, $nor Element $exists, $type Evaluation $mod, $regex, $where Geospatial $geoWithin, $geoIntersects, $near, $nearSphere Array $all, $elemMatch, $size Projection $, $elemMatch, $slice
  24. 24. Updating a document > db.users.find({age : {$gt : 25}}, {_id : 0}) { "name" : "maria", "age" : 26 } { "name" : "marcelo", "age" : 29 } > >db.users.update({age : {$gt : 25}}, {$set : {roles : ['admin', 'dev', 'operator']}})
  25. 25. Removing a document > db.users.remove({name : 'maria'}) > db.users.find().pretty() { "_id" : ObjectId("526f1cb3dac0a62cdc152a98"), "age" : 29, "name" : "marcelo", "roles" : [ "admin", "dev", "operator" ] }
  26. 26. Indexing
  27. 27. Querying a large collection without index > db.estabelecimentos.count() 307929 > > db.estabelecimentos.find({'localizacao.cidade' : 'PIACATU'}).explain() { "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 5, "nscannedObjects" : 307929, "nscanned" : 307929, "nscannedObjectsAllPlans" : 307929, "nscannedAllPlans" : 307929, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 1, "nChunkSkips" : 0, "millis" : 311, "indexBounds" : { } > }, "server" : "Cenerino-PC:27017"
  28. 28. Showing collection’s indexes > db.estabelecimentos.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mapa-servicos.estabelecimentos", "name" : "_id_" } ] >
  29. 29. Creating an index > // creating an index > db.estabelecimentos.ensureIndex({'localizacao.cidade' : 1}) > db.estabelecimentos.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mapa-servicos.estabelecimentos", "name" : "_id_" }, { "v" : 1, "key" : { "localizacao.cidade" : 1 }, "ns" : "mapa-servicos.estabelecimentos", "name" : "localizacao.cidade_1" } ] >
  30. 30. Same query, now using index > db.estabelecimentos.find({'localizacao.cidade' : 'PIACATU'}).explain() { "cursor" : "BtreeCursor localizacao.cidade_1", "isMultiKey" : false, "n" : 5, "nscannedObjects" : 5, "nscanned" : 5, "nscannedObjectsAllPlans" : 5, "nscannedAllPlans" : 5, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "localizacao.cidade" : [ [ "PIACATU", "PIACATU" ] ] }, "server" : "Cenerino-PC:27017" }
  31. 31. Geospatial index > db.estabelecimentos.ensureIndex({'localizacao.coordenadas' : '2dsphere'}) > db.estabelecimentos.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "mapa-servicos.estabelecimentos", "name" : "_id_" }, { "v" : 1, "key" : { "localizacao.cidade" : 1 }, "ns" : "mapa-servicos.estabelecimentos", "name" : "localizacao.cidade_1" }, { "v" : 1, "key" : { "localizacao.coordenadas" : "2dsphere" }, "ns" : "mapa-servicos.estabelecimentos", "name" : "localizacao.coordenadas_2dsphere" } ]
  32. 32. Geospatial index > lng = -46.80208830000004 > lat = -23.515985699999998 > distance = 30 / 6378.137 > > db.estabelecimentos.find({ "localizacao.coordenadas" : { "$nearSphere" : [lng , lat] , "$maxDistance" : distance}}).limit(50) http://mapa-servicos-publicos.herokuapp.com/
  33. 33. Aggregation Framework
  34. 34. Aggregation Framework Pipeline Operators: $project, $match, $limit, $skip, $unwind, $group, $sort, $geoNear
  35. 35. Aggregation Framework > db.estabelecimentos.aggregate([{$match : {'localizacao.uf' : 'SP'}}, {$group : {_id : '$localizacao.cidade', qtd : {$sum : 1}}}, {$sort : {qtd : -1}}, {$limit : 3}]) { "result" : [ { "_id" : "SAO PAULO", "qtd" : 6930 }, { "_id" : "CAMPINAS", "qtd" : 881 }, { "_id" : "GUARULHOS", "qtd" : 666 } ], "ok" : 1 } >
  36. 36. Mongo Driver for Java
  37. 37. Spring Data MongoDB http://projects.spring.io/spring-data-mongodb/
  38. 38. Replica Sets
  39. 39. Replica Sets • A replica set is a group of mongod instances that host the same data set • Replication provides redundancy and increases data availability • The primary accepts all write operations from clients (only one primary allowed) • Replication can be used to increase read capacity • Asynchronous replication • Automatic failover
  40. 40. Replica Sets
  41. 41. Replica Sets
  42. 42. Sharding
  43. 43. Sharding Issues of scaling: • High query rates can exhaust the CPU capacity of the server • Larger data sets exceed the storage capacity of a single machine • Working set sizes larger than the system’s RAM stress the I/O capacity of disk drives Vertical scaling X Sharding
  44. 44. Sharding Horizontally Scalable • Sharding is the process of storing data across multiple machines • Each shard is an independent database, and collectively, the shards make up a single logical database
  45. 45. Sharded clusters
  46. 46. Range Based Sharding • Supports more efficient range queries • Results in an uneven distribution of data • Monotonically increasing keys should be avoided
  47. 47. Hash Based Sharding Ensures an even distribution of data at the expense of efficient range queries
  48. 48. https://education.mongodb.com/
  49. 49. Books
  50. 50. db.audience.find({‘question’ : true})
  51. 51. Thanks everyone! Hope you’ve enjoyed it.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×