mongodb and play! framework workshop
DESCRIPTION
These slides were made for a 3 hour workshop integrate in the first edition of Startup ScholarshipTRANSCRIPT
Startup tech arsenal!
João Vazão Vasques
AGENDA
PART I - MongoDB
MongoDB topics
● Basic concepts
● Operations
● Data modeling
● Performance and tools
Basic concepts 1-2
● NoSQL
● No transactions○ Eventual consistency
● No schema○ Flexibility: code defines your schema
● Scaling (Horizontal scaling)
● Document oriented○ BJSON
Basic concepts 2-2
Database
Table
Indexes
Columns
Row
Database
Collections
Indexes
Fields
Documents
Relational MongoDB
So we have...
Databases containing collections. A collection is made up of documents. Each document is made up of fields
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,
},}
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} } ] } )
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...
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
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
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
● 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
Considerations:
● Geography
● System errors○ ensure your backups can "survive"
● Production constraints○ Schedule according to system usage
● Database configuration○ Replication & Sharding
Backups 1 -3
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
Backup and restore example
● Backup database
> mongodump -v -d test -o dump
● Restore database
> mongorestore -v --db test dump
Backups 3-3
● 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
END OF PART I
Part II - Coding Time!
Part II - Coding Time!
Weapons we'll be using
● Web framework written in Scala & Java
● MVC paradigm
● Built on Akka
● Built in testing tools
● IDE support (Eclipse and IntelliJ IDEA)
About Play!
MVCModel - View - Controller
Anatomy of a Play! application
Coding samplesrouting
Coding samplestemplate engine
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!!!
● engineering.linkedin.com
● blog.twitter.com/engineering
● facebook.com/Engineering
● engineering.foursquare.com
● nerds.airbnb.com
Cool tech blogs
Thank you!