MongoDB NYC Python

2,914 views

Published on

MongoDB presentation for NYC Python's June meetup. Brief discussion on non-relational databases in general followed by an example of using MongoDB as a blog's backend

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,914
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
63
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

MongoDB NYC Python

  1. 1. open-source, high-performance, schema-free, document-oriented database
  2. 2. RDBMS • Great for many applications • Shortcomings • Scalability • Flexibility
  3. 3. CAP Theorem • Consistency • Availability • Tolerance to network Partitions • Pick two http://www.cs.berkeley.edu/~brewer/cs262b-2004/PODC-keynote.pdf
  4. 4. ACID vs BASE • Atomicity • Basically Available • Consistency • Soft state • Isolation • Eventually consistent • Durability
  5. 5. Schema-free • Loosening constraints - added flexibility • Dynamically typed languages • Migrations
  6. 6. • Focus on performance • Rich dynamic queries • Secondary indexes • Replication / failover • Auto-sharding • Many platforms / languages supported
  7. 7. Good at • The web • Caching • High volume / low value • Scalability
  8. 8. Less good at • Highly transactional • Ad-hoc business intelligence • Problems that require SQL
  9. 9. PyMongo • Python driver for MongoDB • Pure Python, with optional C extension • Installation (setuptools): easy_install pymongo
  10. 10. Document • Unit of storage (think row) • Just a dictionary • Can store many Python types: • None, bool, int, float, string / unicode, dict, datetime.datetime, compiled re • Some special types: • SON, Binary, ObjectId, DBRef
  11. 11. Collection • Schema-free equivalent of a table • Logical groups of documents • Indexes are per-collection
  12. 12. _id • Special key • Present in all documents • Unique across a Collection • Any type you want
  13. 13. Blog back-end
  14. 14. Post {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “my blog post...”, “tags”: [“mongodb”, “python”]}
  15. 15. Comment {“author”: “eliot”, “date”: datetime.datetime.utcnow(), “text”: “great post!”}
  16. 16. New post post = {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “my blog post...”, “tags”: [“mongodb”, “python”]} post_id = db.posts.save(post)
  17. 17. Embedding a comment c = {“author”: “eliot”, “date”: datetime.datetime.utcnow(), “text”: “great post!”} db.posts.update({“_id”: post_id}, {“$push”: {“comments”: c}})
  18. 18. Last 10 posts query = db.posts.find() .sort(“date”, DESCENDING) .limit(10) for post in query: print post[“text”]
  19. 19. Posts by author db.posts.find({“author”: “mike”})
  20. 20. Posts in the last week last_week = datetime.datetime.utcnow() + datetime.timedelta(days=-7) db.posts.find({“date”: {“$gt”: last_week}})
  21. 21. Posts ending with ‘Python’ db.posts.find({“text”: re.compile(“Python$”)})
  22. 22. Posts with a tag db.posts.find({“tag”: “mongodb”}) ... and fast db.posts.create_index(“tag”, ASCENDING)
  23. 23. Counting posts db.posts.count() db.posts.find({“author”: “mike”}).count()
  24. 24. Basic paging page = 2 page_size = 15 db.posts.find().limit(page_size) .skip(page * page_size)
  25. 25. Migration: adding titles • Easy - just start adding them: post = {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “another blog post...”, “tags”: [“meetup”, “python”], “title”: “Document Oriented Dbs”} post_id = db.posts.save(post)
  26. 26. Advanced queries • $gt, $lt, $gte, $lte, $ne, $all, $in, $nin • where() db.posts.find().where(“this.author == ‘mike’”) • group()
  27. 27. Other cool stuff • Capped collections • Unique indexes • Mongo shell • GridFS • MongoKit (on pypi)
  28. 28. • Download MongoDB http://www.mongodb.org • Install PyMongo • Try it out!
  29. 29. • http://www.mongodb.org • irc.freenode.net#mongodb • mongodb-user on google groups • @mongodb, @mdirolf • mike@10gen.com • http://www.slideshare.net/mdirolf
  30. 30. • Differences • MVCC based • Replication as path to scalability • Query through predefined views • ACID • REST

×