MongoDB at ZPUGDC
Upcoming SlideShare
Loading in...5
×
 

MongoDB at ZPUGDC

on

  • 6,550 views

Talk about MongoDB, PyMongo and integrating MongoDB w/ Django from the DC Python Meetup group.

Talk about MongoDB, PyMongo and integrating MongoDB w/ Django from the DC Python Meetup group.

Statistics

Views

Total Views
6,550
Views on SlideShare
5,095
Embed Views
1,455

Actions

Likes
7
Downloads
115
Comments
0

4 Embeds 1,455

http://nosql.mypopescu.com 1440
http://www.slideshare.net 9
http://translate.googleusercontent.com 5
http://www.mefeedia.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • blog post <br /> twitter

MongoDB at ZPUGDC MongoDB at ZPUGDC Presentation Transcript

  • open-source, high-performance, schema-free, document-oriented database http://www.mongodb.org/
  • “One size fits all” no longer RDBMS (Oracle, MySQL) Non-relational New gen. OLAP (vertica, aster, greenplum) operational stores (“NoSQL”)
  • NoSQL really means: non-relational next generation operational datastores and databases
  • Scaling out no joins + light transactional semantics = horizontally scalable architectures
  • Data models no joins + light transactional semantics = horizontally scalable architectures important side effect : new data models = improved ways to develop applications
  • Documents and BSON {“hello”: “world”} {“hello”: “world”, “foo”: [{“bar”: 1}]}
  • Schema-free collections • Dynamically typed languages • Migrations • Still define indexes per collection
  • Dynamic queries • Administration • Ease of development / familiarity
  • Atomic update modifiers
  • Focus on performance
  • Replication Replica sets Master - slave master slave master master slave slave slave slave master master slave slave master
  • Auto-sharding Shards mongod mongod mongod ... Config mongod mongod mongod Servers mongod mongod mongod mongos mongos ... client
  • Many supported languages and platforms
  • Good at • The web • Caching • High volume data • Scalability
  • Less good at • Highly transactional • Ad-hoc business intelligence • Problems that require SQL
  • _id • Special key • Present in all documents • Unique across a Collection • Any type you want
  • Post {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “my blog post...”, “tags”: [“mongodb”, “python”]}
  • Comment {“author”: “eliot”, “date”: datetime.datetime.utcnow(), “text”: “great post!”}
  • New post post = {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “my blog post...”, “tags”: [“mongodb”, “python”]} post_id = db.posts.save(post)
  • Embedding a comment c = {“author”: “eliot”, “date”: datetime.datetime.utcnow(), “text”: “great post!”} db.posts.update({“_id”: post_id}, {“$push”: {“comments”: c}})
  • Posts by author db.posts.find({“author”: “mike”})
  • Last 10 posts db.posts.find() .sort(“date”, DESCENDING) .limit(10)
  • Posts in the last week last_week = datetime.datetime.utcnow() + datetime.timedelta(days=-7) db.posts.find({“date”: {“$gt”: last_week}})
  • Posts ending with ‘Python’ db.posts.find({“text”: re.compile(“Python$”)})
  • Posts with a tag db.posts.find({“tags”: “mongodb”}) ... and fast db.posts.create_index(“tags”)
  • Counting posts db.posts.count() db.posts.find({“author”: “mike”}).count()
  • Basic paging page = 2 page_size = 15 db.posts.find().limit(page_size) .skip(page * page_size)
  • Migration: adding titles • Easy - just start adding them: post = {“author”: “mike”, “date”: datetime.datetime.utcnow(), “text”: “another blog post...”, “tags”: [“meetup”, “python”], “title”: “MongoDB @ DC Python”} post_id = db.posts.save(post)
  • Advanced queries • $gt, $lt, $gte, $lte, $ne, $all, $in, $nin • where() db.posts.find().where(“this.author == ‘mike’ || this.title == ‘hello’”) • group()
  • Other cool stuff • Aggregation and map reduce • Capped collections • Unique indexes • Mongo shell • GridFS
  • ?
  • Yes... ...but also sometimes no
  • Similar to + • A lot of Django doesn’t depend on django.db: • URL dispatch, templates, I18N, caching, etc. • Some things do: • Models • Auth • Sessions • Admin
  • settings.py DATABASE_ENGINE = '' DATABASE_NAME = '' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = '' MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', # 'django.contrib.sessions.middleware.SessionMiddleware', # 'django.contrib.auth.middleware.AuthenticationMiddleware', ) INSTALLED_APPS = ( # 'django.contrib.auth', 'django.contrib.contenttypes', # 'django.contrib.sessions', 'django.contrib.sites', )
  • Representing a Poll {'question': 'Do MongoDB + Django <3 each other?', 'pub_date': datetime.datetime(2010, 1, 21), 'choices': [{'votes': 35, 'choice': 'Yes!'}, {'votes': 2, 'choice': 'No...'}]}
  • models.py (PyMongo) def save_poll(question): return db.polls.insert({"question": question, "pub_date": datetime.utcnow()}) def all_polls(): return db.polls.find() def add_choice(poll_id, choice): db.polls.update({"_id": poll_id}, {"$push": {"choices": {"choice": choice, "votes": 0}}}) def add_vote(poll_id, choice): db.polls.update({"_id": poll_id}, {"$inc": {"choices.%d.votes" % choice: 1}}) http://api.mongodb.org/python
  • models.py (MongoKit) class Poll(mongokit.Document): structure = {"question": str, "pub_date": datetime, "choices": [{"choice": str, "votes": int}]} required_fields = ["question"] default_values = {"pub_date": datetime.utcnow} http://bytebucket.org/namlook/mongokit
  • models.py (Ming) class Poll(ming.Document): class __mongometa__: session = session name = "polls" _id = ming.Field(ming.schema.ObjectId) question = ming.Field(str, required=True) pub_date = ming.Field(datetime.datetime, if_missing=datetime.datetime.utcnow) choices = ming.Field([{"choice": str, "votes": int}]) http://merciless.sourceforge.net/
  • mango - sessions and auth • Full sessions support • mango provided User class • supports is_authenticated(), set_password(), etc. http://github.com/vpulim/mango
  • mango - sessions and auth SESSION_ENGINE = 'mango.session' AUTHENTICATION_BACKENDS = ('mango.auth.Backend',) MONGODB_HOST = 'localhost' MONGODB_PORT = None MONGODB_NAME = 'mydb' http://github.com/vpulim/mango
  • What about admin? • No great solution... yet. • Could replace admin app like mango does for sessions / auth • Or...
  • Supporting MongoDB in django.db • Best solution (long term) • http://bitbucket.org/kpot/django-mongodb/ • http://bitbucket.org/wkornewald/django-nonrel/ • http://code.djangoproject.com/wiki/NonSqlBackends
  • 1. Download MongoDB http://www.mongodb.org 2. Try it out! 3. Let us know what you think.
  • NoSQL Live from Boston Thursday, March 11 http://www.10gen.com/events
  • • http://api.mongodb.org/python • http://www.mongodb.org • irc.freenode.net#mongodb • mongodb-user on google groups • @mongodb, @mdirolf • mike@10gen.com