mongodb and play! framework workshop
DESCRIPTION
These slides were made for a 3 hour workshop integrate in the first edition of Startup ScholarshipTRANSCRIPT
![Page 1: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/1.jpg)
Startup tech arsenal!
João Vazão Vasques
![Page 3: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/3.jpg)
AGENDA
![Page 4: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/4.jpg)
PART I - MongoDB
![Page 5: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/5.jpg)
MongoDB topics
● Basic concepts
● Operations
● Data modeling
● Performance and tools
![Page 6: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/6.jpg)
Basic concepts 1-2
● NoSQL
● No transactions○ Eventual consistency
● No schema○ Flexibility: code defines your schema
● Scaling (Horizontal scaling)
● Document oriented○ BJSON
![Page 7: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/7.jpg)
Basic concepts 2-2
Database
Table
Indexes
Columns
Row
Database
Collections
Indexes
Fields
Documents
Relational MongoDB
![Page 8: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/8.jpg)
So we have...
Databases containing collections. A collection is made up of documents. Each document is made up of fields
![Page 9: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/9.jpg)
Operations
● Document example (Uniplaces listing){
"title": "Uniplaces Mega mansion","rentable": false,"location" : {
"city" : "Lisboa","coordinates" : {
"longitude" : -9.145782599999961,"latitude" : 38.7234037
}},"apartment_details" : {
"bathrooms" : 2,"bedrooms" : 6,
},}
![Page 10: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/10.jpg)
Operations
Insert listings - insert
db.listings.insert( { title: "Erasmus Residence", rentable: true } )
db.listings.insert( {title: "xpto", rentable: true, location: [{city: "lisbon", coordinates: {latitude: 0, longitude: 90} } ] } )
![Page 11: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/11.jpg)
Operations
Update listings - update
Syntax: update( < query>, <update>, <options>)
Problem!
We want to make "Mansion" rentable.
Solution (easy)!
db.listings.update( { title: "Uniplaces Mega Mansion"}, {rentable: true } )
Search for Mega Mansion and...
![Page 12: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/12.jpg)
![Page 13: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/13.jpg)
Operations
Update listings 2 -2
Reason: second parameter replaces the original. We replaced the entire document with a new one. Ups!
Solution - $set
Used when you want to change a value of one, or a few, fields
db.listings.update( { title: "Uniplaces Mega Mansion"}, { $set: {rentable: true } } )
Other update modifiers: $inc, $unset, $push, $pop
![Page 14: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/14.jpg)
Search listings - find (1 - 2)
Syntax: find( < query>, <projection>)
Projectiondb.listings.find( { title: "Uniplaces Mega mansion"}, {rentable: 1} )
Logical operators ($and)db.listings.find({ $and: [{ title: "Uniplaces Mega Mansion"}, {rentable: "false"}] })
Comparison operators ($gte)db.listings.find( { "apartment_details.bedrooms" : {$gte: 2 } }, { title: 1} )
Operations
![Page 15: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/15.jpg)
Search listings - find (2 - 2)
Ordering (sort: -1 descending, 1 ascending)
db.listings.find({ $gte: { "apartment_details.bedrooms" : 2 }).sort("apartment_details.bedrooms": -1)
Paging (limit and skip)
db.listings.find().limit(3).skip(1)
Operations
![Page 16: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/16.jpg)
● MongoDB does not support Joins● Embedded Documents{
"title": "Uniplaces Mega mansion",
"bedrooms" : [
{
"price" : 500,
"area": 25
},
{
"price" : 700,
"area": 30
},
]
}
db.listings.find( {bedrooms: { $elemMatch: {price: { $gt: 20 }, area: 25} } })
Data modeling
![Page 17: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/17.jpg)
Considerations:
● Geography
● System errors○ ensure your backups can "survive"
● Production constraints○ Schedule according to system usage
● Database configuration○ Replication & Sharding
Backups 1 -3
![Page 18: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/18.jpg)
Approaches to backup MongoDB systems
● Binary database dumps○ Small○ Don't include index content and padding○ Cannot reflect single moment in time
● Filesystem snapshots○ Large backup sizes○ Accurate○ Require filesystem and OS tools○ Can reflect a single moment in time
■ Note: in sharding all writes must be suspended
Backups 2-3
![Page 19: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/19.jpg)
Backup and restore example
● Backup database
> mongodump -v -d test -o dump
● Restore database
> mongorestore -v --db test dump
Backups 3-3
![Page 20: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/20.jpg)
● Writes to primary (only)
○ A write is fully committed once it has replicated to a majority of servers in the set
● Reads to primary and secondary
● Consensus election in case of primary fail
Replica Sets
Secondary
Secondary
PrimaryW
W
W
SecondaryW
![Page 21: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/21.jpg)
END OF PART I
![Page 22: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/22.jpg)
Part II - Coding Time!
![Page 23: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/23.jpg)
Part II - Coding Time!
Weapons we'll be using
![Page 24: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/24.jpg)
● Web framework written in Scala & Java
● MVC paradigm
● Built on Akka
● Built in testing tools
● IDE support (Eclipse and IntelliJ IDEA)
About Play!
![Page 25: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/25.jpg)
MVCModel - View - Controller
![Page 26: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/26.jpg)
Anatomy of a Play! application
![Page 27: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/27.jpg)
Coding samplesrouting
![Page 28: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/28.jpg)
Coding samplestemplate engine
![Page 29: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/29.jpg)
Download the demo project on Github
● https://github.com/JoaoVasques/mongodb-workshop
Challenges:
● Fire Employee● Search employee● Create a backup of the database and restore it
Hands on time!!!
![Page 30: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/30.jpg)
● engineering.linkedin.com
● blog.twitter.com/engineering
● facebook.com/Engineering
● engineering.foursquare.com
● nerds.airbnb.com
Cool tech blogs
![Page 31: MongoDB and Play! Framework workshop](https://reader034.vdocuments.net/reader034/viewer/2022042518/54be4dbb4a7959776b8b458a/html5/thumbnails/31.jpg)
Thank you!