Skip to content

Instantly share code, notes, and snippets.

@ldong
Created June 17, 2014 17:01
Show Gist options
  • Save ldong/e862d9e1fe89982f43ca to your computer and use it in GitHub Desktop.
Save ldong/e862d9e1fe89982f43ca to your computer and use it in GitHub Desktop.
Tech Talk for Ming and PyMongo

June 17 Tech Talk

Author: Rick Copeland

Ming

What is Ming? MongoDB driver

MongoDB

MongoDB Terminology

Relational MongoDB
Database Databse
Table Collection
Index Index
Row Document
Column Field

BSON: binary format for JSON

Example:

  1. Document Primary Key: ID in Relational DB
  2. UnixDateTime: ISODateTime('2012-09-17')

db.update({spec}, {update})

MongoDB indexing

  1. At most one index is used for any given query/update
  2. Most indexes are B-tree based
  3. GeoSpatial indexes and queryies
  4. Full-text search

Scaling Mongdb

Mongodb uses shards

3 configurations

Drivers

PyMongo

  1. Auto-generated _id on client-sides
  2. Cursor == Python Generator
  3. CRUD
  4. .explain() will expalin the query cursors
  5. Aggregation

Why Ming?

  1. Data has a schema
  2. Migration
  3. Unit of work -- queue up the updates

Datastore and Session

  1. Define Schema
  2. Ming Models

Ming Bounus: MIM

  1. in memory partial pymongo implmentation
  2. Useful for unit tests, Does NOT actually touch the disk db
  3. Does NOT Scale

Ming ODM

  1. Session -> ODMSession
  2. collection.m ... -> MappedClass.query
  3. Session actually does stuff
    • Track object identity
    • Track object modifications
    • Unit of work to save everything at once

Integration with python web frameworks

  1. ThreadLocalODMSession
  2. ming.odm.middleware.MingMiddleware
    • flush
    • clear

Decorators

@something
@something_else
def foo():
  pass
  
def foo():
  pass
something_else(foo)
something(foo)

Another example:

@view(..)
def my_view():
    ...

def view(a, b):
    def decorator(function):
        function.a = a
        function.b = b
        return function
    return decorator

def log(logger, message):
    def decorator(function):
        def wrapper(*args, **kwargs):
            logger.info('ENTER:' + message)
            try:
                result = function(*args, **kwargs)
                logger.info('EXIT (%r): ' + message, result)
                return result
            except Exception as err:
                logger.info('EXCEPTION (%r): ' + message, err)
                raise
        return wrapper
    return decorator

@log(logging.getLogger(__name__), 'Mylog')
def something(a,b):
    pass

def something
_tmp0 = log(...)
something = _tmp0(something)


something == wrapper<...>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment