webinar: build an application series - session 3 - interacting with the database

33
Application Development Series Back to Basics Interacting with the database Daniel Roberts @dmroberts #MongoDBBasics

Upload: mongodb

Post on 09-May-2015

3.044 views

Category:

Technology


0 download

DESCRIPTION

The third session in our eight-part webinar series on "Building an application with MongoDB" to learn best practices, tips and tricks from our Solution Architects and learn how easy it is to start building applications with MongoDB. This session - presented by Daniel Roberts, Solutions Architect at MongoDB - discusses queries and updates and the interaction between an application and a database. Next in the Series: March 6th 2014 - Build an Application Series - Session 4 - Indexing This session will focus on indexing strategies for the application, including geo spatial and full text search March 20th 2014 - Build an Application Series - Session 5 - Reporting in your application This session covers Reporting and Aggregation Framework and Building application usage reports April 3th 2014 - Operations for your application - Session 6 - Deploying the application By this stage, we will have built the application. Now we need to deploy it. We will discuss architecture for High Availability and scale out April 17th 2014 - Operations for your application - Session 7 - Backup and DR This webinar will discuss back up and restore options. Learn what you should do in the event of a failure and how to perform a backup and recovery of the data in your applications May 6th 2014 - Operations for your application - Session 8 - Monitoring and Performance Tuning The final webinar of the series will discuss what metrics are important and how to manage and monitor your application for key performance. Daniel Roberts: About the speaker Daniel Roberts is a Solutions Architect based in London. Prior to MongoDB Daniel worked at Oracle for 11 years in a number of different positions, including Oracle's middleware technologies and strategy. Prior roles include consulting, product management, business development and more recently as a solution architect for financial services. Daniel has also worked for Novell, ICL and as a freelance contractor. He has a degree in Computer Science from Nottingham Trent University in the UK.

TRANSCRIPT

Page 1: Webinar: Build an Application Series - Session 3 - Interacting with the database

Application Development SeriesBack to BasicsInteracting with the database

Daniel Roberts@dmroberts

#MongoDBBasics

Page 2: Webinar: Build an Application Series - Session 3 - Interacting with the database

2

• Recap from last session

• MongoDB Inserts & Queries– ObjectId– Returning documents – cursors– Projections

• MongoDB Update operators– Fixed Buckets– Pre Aggregated Reports

• Write Concern– Durability vs Performance trade off

Agenda

Page 3: Webinar: Build an Application Series - Session 3 - Interacting with the database

3

• Virtual Genius Bar

– Use the chat to post questions

– EMEA Solution Architecture / Support team are on hand

– Make use of them during the sessions!!!

Q & A

Page 4: Webinar: Build an Application Series - Session 3 - Interacting with the database

Recap from last time….

Page 5: Webinar: Build an Application Series - Session 3 - Interacting with the database

5

• Looked at the application architecture– JSON / RESTful– Python based

Architecture

Client-sideJSON

(eg AngularJS) (BSON)

Pymongo driver

Python web app

HTTP(S) REST

Page 6: Webinar: Build an Application Series - Session 3 - Interacting with the database

6

• Schema design– Modeled

• Articles• Comments• Interactions• Users

Schema and Architecture

Page 7: Webinar: Build an Application Series - Session 3 - Interacting with the database

7

Modeling Articles

• Posting articles• insert

• Get List of articles• Return Cursor

• Get individual article

{ '_id' : ObjectId(...),

'text': 'Article content…',

'date' : ISODate(...),

'title' : ’Intro to MongoDB',

'author' : 'Dan Roberts',

'tags' : ['mongodb',

'database',

'nosql’]

}

Articles collection

METHODSdef get_article(article_id)def get_articles():def create_article():

Page 8: Webinar: Build an Application Series - Session 3 - Interacting with the database

8

Modeling Comments

• Storing comments

• Quickly retrieve most recent comments

• Add new comments to document

• ‘Bucketing’

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘page’ : 1, ‘count’ : 42 ‘comments’ : [

{ ‘text’ : ‘A great article, helped me understand schema design’, ‘date’ : ISODate(..), ‘author’ : ‘johnsmith’ }, …}

Comments collection

METHODSdef add_comment(article_id):def get_comments(article_id):

Page 9: Webinar: Build an Application Series - Session 3 - Interacting with the database

9

Modeling Interactions

• Used for reporting on articles

• Create “pre-aggregated” reports

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘section’ : ‘schema’,

‘date’ : ISODate(..),‘daily’: { ‘views’ : 45,

‘comments’ : 150 } ‘hours’ : { 0 : { ‘views’ : 10 }, 1 : { ‘views’ : 2 }, … 23 : { ‘views’ : 14,

‘comments’ : 10 } }}

Interactions collection

METHODS def add_interaction(article_id, type):

Page 10: Webinar: Build an Application Series - Session 3 - Interacting with the database

Inserting / Querying

Page 11: Webinar: Build an Application Series - Session 3 - Interacting with the database

11

>db.articles.insert({'text': 'Article content…’,

'date' : ISODate(...), 'title' : ’Intro to MongoDB’, 'author' : 'Dan Roberts’, 'tags' : [ 'mongodb',

'database', 'nosql’

]});

• Driver generates ObjectId() for _id – if not specified– 12 bytes - 4-byte epoch, 3-byte machine id, a 2-byte process id, and a 3-byte

counter.

Inserting documents

Page 12: Webinar: Build an Application Series - Session 3 - Interacting with the database

12

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

• Use to query documents

• Logical: $or, $and, $not, $nor Element: $exists, $type

• Evaluation: $mod, $regex, $where Geospatial: $geoWithin, $geoIntersects, $near, $nearSphere

Comparison Operators

db.articles.find( { 'title' : ’Intro to MongoDB’ } )

db.articles.find( { ’date' : { ‘$lt’ : {ISODate("2014-02-

19T00:00:00.000Z") }} )

db.articles.find( { ‘tags’ : { ‘$in’ : [‘nosql’, ‘database’] } } );

Page 13: Webinar: Build an Application Series - Session 3 - Interacting with the database

13

• Find returns a cursor– Use to iterate over the results– cursor has many methods

Cursors

>var cursor = db.articles.find ( { ’author' : ’Dan Roberts’ } )>cursor.hasNext()true>cursor.next(){ '_id' : ObjectId(...),

'text': 'Article content…’, 'date' : ISODate(...), 'title' : ’Intro to MongoDB’, 'author' : 'Dan Roberts’, 'tags' : [ 'mongodb', 'database’, 'nosql’ ]

}

Page 14: Webinar: Build an Application Series - Session 3 - Interacting with the database

14

• Return only the attributes needed– Boolean 0 or 1 syntax select attributes– Improved efficiency

Projections

>var cursor = db.articles.find( { ’author' : ’Dan Roberts’ } , {‘_id’:0, ‘title’:1})>cursor.hasNext()true>cursor.next(){ "title" : "Intro to MongoDB" }

Page 15: Webinar: Build an Application Series - Session 3 - Interacting with the database

Updates

Page 16: Webinar: Build an Application Series - Session 3 - Interacting with the database

16

$each, $slice, $sort, $inc, $push$inc, $rename, $setOnInsert, $set, $unset, $max, $min

$, $addToSet, $pop, $pullAll, $pull, $pushAll, $push

$each, $slice, $sort

Update Operators

>db.articles.update(

{ '_id' : ObjectId(...)},

{ '$push' : {'comments' : ‘Great

article!’ }}

)

{ 'text': 'Article content…’ 'date' : ISODate(...),

'title' : ’Intro to MongoDB’,

'author' : 'Dan Roberts’,'tags' : ['mongodb',

'database’,'nosql’ ],’comments' :

[‘Great article!’ ]

}

Page 17: Webinar: Build an Application Series - Session 3 - Interacting with the database

17

Push to a fixed size array with…

$push, $each, $slice

Update Operators

>db.articles.update(

{ '_id' : ObjectId(...)},

{ '$push' : {'comments' :

{

'$each' : [‘Excellent’], '$slice' : -3}}, })

{ 'text': 'Article content…’ 'date' : ISODate(...),

'title' : ’Intro to MongoDB’,

'author' : 'Dan Roberts’,'tags' : ['mongodb',

'database’,'nosql’ ],’comments' :

[‘Great article!’, ‘More please’, ‘Excellent’ ]

}

Page 18: Webinar: Build an Application Series - Session 3 - Interacting with the database

18

• Push 10 comments to a document (bucket).

• Automatically create a new document.

• Use {upsert: true} instead of insert.

Update Operators - Bucketing

>db.comments.update(

{‘c’: {‘$lt’:10}}, {

‘$inc’ : {c:1}, '$push' : {

'comments' : ‘Excellent’}},{ upsert : true }

)

{‘_id’ : ObjectId( … )‘c’ : 3,’comments' :

[‘Great article!’,

‘More please’, ‘Excellent’ ]

}

Page 19: Webinar: Build an Application Series - Session 3 - Interacting with the database

19

Analytics – Pre-Aggregated reports

• Used for reporting on articles

• Create “pre-aggregated” reports

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘section’ : ‘schema’,

‘date’ : ISODate(..),‘daily’: { ‘views’ : 45,

‘comments’ : 150 } ‘hours’ : { 0 : { ‘views’ : 10 }, 1 : { ‘views’ : 2 }, … 23 : { ‘views’ : 14,

‘comments’ : 10 } }}

Interactions collections

METHOD def add_interaction(article_id, type):

Page 20: Webinar: Build an Application Series - Session 3 - Interacting with the database

20

• Use $inc to increment multiple counters.

• Single Atomic operation.

• Increment daily and hourly counters.

Incrementing Counters

>db.interactions.update({‘article_id’ : ObjectId(..)}, {

‘$inc’ : {

‘daily.views’:1,

‘daily.comments’:1

‘hours.8.views’:1

‘hours.8.comments’:1 })

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘section’ : ‘schema’,

‘date’ : ISODate(..),‘daily’: { ‘views’ : 45,

‘comments’ : 150 } ‘hours’ : { 0 : { ‘views’ : 10 }, 1 : { ‘views’ : 2 }, … 23 : { ‘views’ : 14,

‘comments’ : 10 } }}

Page 21: Webinar: Build an Application Series - Session 3 - Interacting with the database

21

• Increment new counters

Incrementing Counters 2

>db.interactions.update({‘article_id’ : ObjectId(..)}, {

‘$inc’ : {

‘daily.views’:1,

‘daily.comments’:1,

‘hours.8.views’:1,

‘hours.8.comments’:1,

‘referrers.bing’ : 1})

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘section’ : ‘schema’,

‘date’ : ISODate(..),‘daily’: { ‘views’ : 45,

‘comments’ : 150 } ‘hours’ : {

…..}‘referrers’ : {

‘google’ : 27}

}

Page 22: Webinar: Build an Application Series - Session 3 - Interacting with the database

22

• Increment new counters

Incrementing Counters 2

>db.interactions.update({‘article_id’ : ObjectId(..)}, {

‘$inc’ : {

‘daily.views’:1,

‘daily.comments’:1,

‘hours.8.views’:1,

‘hours.8.comments’:1,

‘referrers.bing’ : 1})

{‘_id’ : ObjectId(..),

‘article_id’ : ObjectId(..), ‘section’ : ‘schema’,

‘date’ : ISODate(..),‘daily’: { ‘views’ : 45,

‘comments’ : 150 } ‘hours’ : {

…..}‘referrers’ : {

‘google’ : 27,‘bing’ : 1

}}

Page 23: Webinar: Build an Application Series - Session 3 - Interacting with the database

Durability

Page 24: Webinar: Build an Application Series - Session 3 - Interacting with the database

24

Durability

• With MongoDB you get to choose• In memory• On disk• Multiple servers

• Write Concerns• Report on success of write operations• getLastError called from driver

• Trade off• Latency of response

Page 25: Webinar: Build an Application Series - Session 3 - Interacting with the database

25

Unacknowledged

Page 26: Webinar: Build an Application Series - Session 3 - Interacting with the database

26

MongoDB Acknowledged

Default Write Concern

Page 27: Webinar: Build an Application Series - Session 3 - Interacting with the database

27

Wait for Journal Sync

Page 28: Webinar: Build an Application Series - Session 3 - Interacting with the database

28

Replica Sets

• Replica Set – two or more copies

• “Self-healing” shard

• Addresses many concerns:

- High Availability

- Disaster Recovery

- Maintenance

Page 29: Webinar: Build an Application Series - Session 3 - Interacting with the database

29

Wait for Replication

Page 30: Webinar: Build an Application Series - Session 3 - Interacting with the database

Summary

Page 31: Webinar: Build an Application Series - Session 3 - Interacting with the database

31

• Interacting with the database

– Queries and projections– Inserts and Upserts

– Update Operators– Bucketing– Pre Aggregated reports

• basis for fast analytics

Summary

Page 32: Webinar: Build an Application Series - Session 3 - Interacting with the database

32

– Indexing• Indexing strategies• Tuning Queries

– Text Search

– Geo Spatial

– Query Profiler

Next Session – 6th March

Page 33: Webinar: Build an Application Series - Session 3 - Interacting with the database