This intermediate-level talk will teach you techniques using the popular NoSQL database MongoDB and the Python library Ming to write maintainable, high-performance, and scalable applications. We will cover everything you need to become an effective Ming/MongoDB developer from basic PyMongo queries to high-level object-document mapping setups in Ming.
7. One Rule (for now): Avoid Javascript
http://www.flickr.com/photos/lizjones/295567490/
8. You gotta write Javascript (for now)
It’s pretty slow (single-threaded JS engine)
MongoDB 2.2 with New
Javascript is used by Aggregation Framework
$where in a query Coming Real Soon Now ™
.group(key, condition, initial, reduce, finalize=None)
.map_reduce(map, reduce, out, finalize=None, …)
Sharding gives some parallelism with .map_reduce() (and
possibly ‘$where’). Otherwise you’re single threaded.
9. >>>importgridfs
>>>fs = gridfs.GridFS(db) Python context
>>>withfs.new_file() asfp: manager
... fp.write('The file')
...
>>>fp
<gridfs.grid_file.GridIn object at 0x2cae910>
>>>fp._id
ObjectId('4e727f64eb03300c0b000003') Retrieve file by _id
>>>fs.get(fp._id).read()
'The file'
Arbitrary data can be stored in the ‘fp’ object – it’s
just a Document (but please put it in ‘fp.metadata’)
Mime type, links to other docs, etc.
11. - Get started with PyMongo
- Sprinkle in some Ming schemas
- ODM: When a dict just won’t do
12. Your data has a schema
Your database can define and enforce it
It can live in your application (as with MongoDB)
Nice to have the schema defined in one place in the code
Sometimes you need a “migration”
Changing the structure/meaning of fields
Adding indexes, particularly unique indexes
Sometimes lazy, sometimes eager
“Unit of work:” Queuing up all your updates can be handy
16. frommingimport schema, Field
WikiDoc= collection(‘wiki_page', session,
Field('_id', schema.ObjectId()),
Index created on
Field('title', str, index=True),
import
Field('text', str))
CommentDoc= collection(‘comment', session,
Field('_id', schema.ObjectId()),
Field('page_id', schema.ObjectId(), index=True),
Field('text', str))
Shorthand for
schema.String
17. frommingimport Document, Session, Field
classWikiDoc(Document):
class__mongometa__:
session=Session.by_name(’main')
name='wiki_page’
indexes=[ ('title') ]
title = Field(str)
text = Field(str)
Old declarative syntax continues to exist and be
supported, but it’s not being actively improved
Sometimes nice when you want additional
methods/attrs on your document class
18. >>>doc = WikiDoc(dict(title='Cats', text='I can hazcheezburger?'))
>>>doc.m.save()
>>>WikiDoc.m.find()
<ming.base.Cursor object at 0x2c2cd90>
>>>WikiDoc.m.find().all() Documents are dict
subclasses
[{'text': u'I can hazcheezburger?', '_id': ObjectId('4e727163eb03300c0b000001'),
'title': u'Cats'}]
>>>WikiDoc.m.find().one().text
u'Ican hazcheezburger?’
>>>doc = WikiDoc(dict(tietul='LOL', text='Invisible bicycle'))
>>>doc.m.save()
Traceback(most recent call last):
File "<stdin>", line 1,
… Exception pinpoints
ming.schema.Invalid: <class 'ming.metadata.Document<wiki_page>'>:
problem
Extra keys: set(['tietul'])
25. Various plug points in the session
before_flush
after_flush
Some uses
Logging changes to sensitive data or for analytics
Full-text search indexing
“last modified” fields
Performance instrumentation
26. Various plug points in the mapper
before_/after_:
Insert
Update
Delete
Remove
Some uses
Collection/model-specific logging (user creation, etc.)
Anything you might want a SessionExtension for but
would rather do per-model
28. Rick Copeland @rick446
Arborian Consulting, LLC
Feedback? http://www.surveymonkey.com/s/5DLCYKN
http://www.flickr.com/photos/f-oxymoron/5005673112/
Editor's Notes
Who’s using mongodb? In production?Who’s using python? In production?Who’s using both together? In Production?I’m a consultant, but I *was* an engineer at SourceForgeI’ve been using MongoDB at SourceForge since 0.8, Python since 2002 or so, love them bothWe extracted our MongoDB libraries into a separate OSS project ‘Ming’ (play off of the planet Mongo from Flash Gordon)We’re going to have lots of code here. Hope you like Python!
Someone is going to put weird data in your database.Or they will try.Catch them in the act.