mongodb and play! framework workshop

31
Startup tech arsenal! João Vazão Vasques

Upload: joao-vazao-vasques

Post on 20-Jan-2015

2.798 views

Category:

Technology


0 download

DESCRIPTION

These slides were made for a 3 hour workshop integrate in the first edition of Startup Scholarship

TRANSCRIPT

Page 1: MongoDB and Play! Framework workshop

Startup tech arsenal!

João Vazão Vasques

Page 2: MongoDB and Play! Framework workshop

ABOUT ME

@JoaoVasques

@JoaoVasques

joao.l.v.vasques

[email protected]

Page 3: MongoDB and Play! Framework workshop

AGENDA

Page 4: MongoDB and Play! Framework workshop

PART I - MongoDB

Page 5: MongoDB and Play! Framework workshop

MongoDB topics

● Basic concepts

● Operations

● Data modeling

● Performance and tools

Page 6: MongoDB and Play! Framework workshop

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

Basic concepts 2-2

Database

Table

Indexes

Columns

Row

Database

Collections

Indexes

Fields

Documents

Relational MongoDB

Page 8: MongoDB and Play! Framework workshop

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

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

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

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
Page 13: MongoDB and Play! Framework workshop

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

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

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

● 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

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

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

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

● 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

END OF PART I

Page 22: MongoDB and Play! Framework workshop

Part II - Coding Time!

Page 23: MongoDB and Play! Framework workshop

Part II - Coding Time!

Weapons we'll be using

Page 24: MongoDB and Play! Framework workshop

● 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

MVCModel - View - Controller

Page 26: MongoDB and Play! Framework workshop

Anatomy of a Play! application

Page 27: MongoDB and Play! Framework workshop

Coding samplesrouting

Page 28: MongoDB and Play! Framework workshop

Coding samplestemplate engine

Page 29: MongoDB and Play! Framework workshop

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

● 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

Thank you!