Application Development Series
Back to Basics
Interacting with the database

Daniel Roberts
@dmroberts
#MongoDBBasics
Agenda
• Recap from last session
• MongoDB Inserts & Queries
– ObjectId
– Returning documents – cursors
– Projections

• M...
Q&A
• Virtual Genius Bar
– Use the chat to post
questions
– EMEA Solution
Architecture / Support
team are on hand
– Make u...
Recap from last time….
Architecture
• Looked at the application architecture
– JSON / RESTful
– Python based

HTTP(S) REST

Client-side
JSON

Pyt...
Schema and Architecture
• Schema design
– Modeled
•
•
•
•

6

Articles
Comments
Interactions
Users
Modeling Articles
Articles collection
def get_article(article_id)
def get_articles():
def create_article():

METHODS

{
'_...
Modeling Comments
Comments collection
METHODS

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

{

• Storing co...
Modeling Interactions
Interactions collection
METHODS

def add_interaction(article_id, type):

{
„_id‟ : ObjectId(..),
„ar...
Inserting / Querying
Inserting documents
• Driver generates ObjectId() for _id
– if not specified
– 12 bytes - 4-byte

epoch, 3-byte machine id...
Comparison Operators
$gt, $gte, $in, $lt, $lte, $ne, $nin
• Use to query documents
db.articles.find( { 'title' : ‟Intro to...
Cursors
• Find returns a cursor
– Use to iterate over the results
– cursor has many methods
>var cursor = db.articles.find...
Projections
• Return only the attributes needed
– Boolean 0 or 1 syntax select attributes
– Improved efficiency

>var curs...
Updates
Update Operators
$each, $slice, $sort, $inc, $push
$inc, $rename, $setOnInsert, $set, $unset, $max, $min
$, $addToSet, $po...
Update Operators
Push to a fixed size array with…

$push, $each, $slice
{
>db.articles.update(

{ '_id' : ObjectId(...)},
...
Update Operators - Bucketing
•

Push 10 comments to a document (bucket).

•

Automatically create a new document.

•

Use ...
Analytics – Pre-Aggregated reports
Interactions collections
METHOD

def add_interaction(article_id, type):

{
„_id‟ : Obje...
Incrementing Counters
•

Use $inc to increment multiple counters.

•

Single Atomic operation.

•

Increment daily and hou...
Incrementing Counters 2
• Increment new counters
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟...
Incrementing Counters 2
• Increment new counters
{
„_id‟ : ObjectId(..),
„article_id‟ : ObjectId(..),
„section‟ : „schema‟...
Durability
Durability
•

With MongoDB you get to choose
•
•
•

•

Write Concerns
•
•

•

Report on success of write operations
getLas...
Unacknowledged

25
MongoDB Acknowledged
Default Write Concern

26
Wait for Journal Sync

27
Replica Sets
• Replica Set – two or more copies
• “Self-healing” shard
• Addresses many concerns:
- High Availability
- Di...
Wait for Replication

29
Summary
Summary
• Interacting with the database
– Queries and projections
– Inserts and Upserts
– Update Operators
– Bucketing
– P...
Next Session – 6th March
– Indexing
• Indexing strategies
• Tuning Queries

– Text Search
– Geo Spatial
– Query Profiler

...
Webinar: Build an Application Series - Session 3 - Interacting with the database
Upcoming SlideShare
Loading in …5
×

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

3,347 views
3,260 views

Published on

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.

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
3,347
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
98
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Not really fire and forget. This return arrow is to confirm that the network successfully transferred the packet(s) of data.This confirms that the TCP ACK response was received.
  • Presenter should mention:Default is w:1w:majority is what most people should use for durability. Majority is a special token here signifying more than half of the nodes in the set have acknowledged the write.
  • Webinar: Build an Application Series - Session 3 - Interacting with the database

    1. 1. Application Development Series Back to Basics Interacting with the database Daniel Roberts @dmroberts #MongoDBBasics
    2. 2. Agenda • 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 2
    3. 3. Q&A • Virtual Genius Bar – Use the chat to post questions – EMEA Solution Architecture / Support team are on hand – Make use of them during the sessions!!! 3
    4. 4. Recap from last time….
    5. 5. Architecture • Looked at the application architecture – JSON / RESTful – Python based HTTP(S) REST Client-side JSON Python web app (BSON) (eg AngularJS) Pymongo driver 5
    6. 6. Schema and Architecture • Schema design – Modeled • • • • 6 Articles Comments Interactions Users
    7. 7. Modeling Articles Articles collection def get_article(article_id) def get_articles(): def create_article(): METHODS { '_id' : 'text': • Posting articles • 'Article content…', 'date' : ISODate(...), insert 'title' : • Get List of articles • ObjectId(...), ‟Intro to MongoDB', 'author' : 'Dan Roberts', Return Cursor 'tags' : • Get individual article [ ] } 7 'mongodb', 'database', 'nosql‟
    8. 8. Modeling Comments Comments collection METHODS def add_comment(article_id): def get_comments(article_id): { • Storing comments • Quickly retrieve most recent comments • Add new comments to document • „Bucketing‟ 8 „_id‟ : ObjectId(..), „article_id‟ : ObjectId(..), „page‟ : 1, „count‟ : 42 „comments‟ : [ { „text‟ : „A great article, helped me understand schema design‟, „date‟ : ISODate(..), „author‟ : „johnsmith‟ }, … }
    9. 9. Modeling Interactions Interactions collection METHODS def add_interaction(article_id, type): { „_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 } } • Used for reporting on articles • Create “preaggregated” reports } 9
    10. 10. Inserting / Querying
    11. 11. Inserting documents • 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. >db.articles.insert({ 'text': 'Article content…‟, 'date' : ISODate(...), 'title' : ‟Intro to MongoDB‟, 'author' : 'Dan Roberts‟, 'tags' : [ 'mongodb', 'database', 'nosql‟ ] }); 11
    12. 12. Comparison Operators $gt, $gte, $in, $lt, $lte, $ne, $nin • Use to query documents 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‟] } } ); • Logical: $or, $and, $not, $nor • Evaluation: $mod, $regex, $where Geospatial: $geoWithin, $geoIntersects, $near, $nearSphere 12 Element: $exists, $type
    13. 13. Cursors • Find returns a cursor – Use to iterate over the results – cursor has many methods >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’ ] } 13 )
    14. 14. Projections • Return only the attributes needed – Boolean 0 or 1 syntax select attributes – Improved efficiency >var cursor = db.articles.find( { ‟author' : ‟Dan Roberts‟ } , {‘_id’:0, ‘title’:1}) >cursor.hasNext() true >cursor.next() { "title" : "Intro to MongoDB" } 14
    15. 15. Updates
    16. 16. Update Operators $each, $slice, $sort, $inc, $push $inc, $rename, $setOnInsert, $set, $unset, $max, $min $, $addToSet, $pop, $pullAll, $pull, $pushAll, $push $each, $slice, $sort >db.articles.update( { { '_id' : ObjectId(...)}, { '$push' : {'comments' : „Great article!’ } } ) 16 } 'text': 'Article content…‟ 'date' : ISODate(...), 'title' : ‟Intro to MongoDB‟, 'author' : 'Dan Roberts‟, 'tags' : ['mongodb', 'database‟,'nosql’ ], ’comments' : [‘Great article!’ ]
    17. 17. Update Operators Push to a fixed size array with… $push, $each, $slice { >db.articles.update( { '_id' : ObjectId(...)}, { '$push' : {'comments' : { '$each' : [„Excellent‟], '$slice' : -3}}, }) 17 } 'text': 'Article content…‟ 'date' : ISODate(...), 'title' : ‟Intro to MongoDB‟, 'author' : 'Dan Roberts‟, 'tags' : ['mongodb', 'database‟,'nosql’ ], ’comments' : [‘Great article!’, ‘More please’, ‘Excellent’ ]
    18. 18. Update Operators - Bucketing • Push 10 comments to a document (bucket). • Automatically create a new document. • Use {upsert: true} instead of insert. >db.comments.update( {„c‟: {„$lt‟:10}}, { „$inc‟ : {c:1}, '$push' : { 'comments' : „Excellent‟ } }, { upsert : true } ) 18 { „_id‟ : ObjectId( … ) „c‟ : 3, ’comments' : [‘Great article!’, ‘More please’, ‘Excellent’ ] }
    19. 19. Analytics – Pre-Aggregated reports Interactions collections METHOD def add_interaction(article_id, type): { „_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 } } • Used for reporting on articles • Create “preaggregated” reports } 19
    20. 20. Incrementing Counters • Use $inc to increment multiple counters. • Single Atomic operation. • Increment daily and hourly counters. { >db.interactions.update( {„article_id‟ : ObjectId(..)}, { „$inc‟ : { „daily.views‟:1, „daily.comments‟:1 „hours.8.views‟:1 „hours.8.comments‟:1 } ) } 20 „_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 } }
    21. 21. Incrementing Counters 2 • Increment new counters { „_id‟ : ObjectId(..), „article_id‟ : ObjectId(..), „section‟ : „schema‟, „date‟ : ISODate(..), „daily‟: { „views‟ : 45, „comments‟ : 150 } „hours‟ : { ….. } „referrers‟ : { „google‟ : 27 } >db.interactions.update( {„article_id‟ : ObjectId(..)}, { „$inc‟ : { „daily.views‟:1, „daily.comments‟:1, „hours.8.views‟:1, „hours.8.comments‟:1, ‘referrers.bing’ : 1 } ) 21 }
    22. 22. Incrementing Counters 2 • Increment new counters { „_id‟ : ObjectId(..), „article_id‟ : ObjectId(..), „section‟ : „schema‟, „date‟ : ISODate(..), „daily‟: { „views‟ : 45, „comments‟ : 150 } „hours‟ : { ….. } „referrers‟ : { „google‟ : 27, ‘bing’ : 1 } >db.interactions.update( {„article_id‟ : ObjectId(..)}, { „$inc‟ : { „daily.views‟:1, „daily.comments‟:1, „hours.8.views‟:1, „hours.8.comments‟:1, ‘referrers.bing’ : 1 } ) } 22
    23. 23. Durability
    24. 24. Durability • With MongoDB you get to choose • • • • Write Concerns • • • Report on success of write operations getLastError called from driver Trade off • 24 In memory On disk Multiple servers Latency of response
    25. 25. Unacknowledged 25
    26. 26. MongoDB Acknowledged Default Write Concern 26
    27. 27. Wait for Journal Sync 27
    28. 28. Replica Sets • Replica Set – two or more copies • “Self-healing” shard • Addresses many concerns: - High Availability - Disaster Recovery - Maintenance 28
    29. 29. Wait for Replication 29
    30. 30. Summary
    31. 31. Summary • Interacting with the database – Queries and projections – Inserts and Upserts – Update Operators – Bucketing – Pre Aggregated reports • basis for fast analytics 31
    32. 32. Next Session – 6th March – Indexing • Indexing strategies • Tuning Queries – Text Search – Geo Spatial – Query Profiler 32

    ×