Upload
rick-copeland
View
4.329
Download
2
Tags:
Embed Size (px)
DESCRIPTION
This talk, given at PyGotham 2011, 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.
Citation preview
2.
3. SourceForges MongoDB
4. What is MongoDB? MongoDB (from "humongous") is a scalable, high-performance,open source, document-oriented database. Sharding, Replication 20k inserts/s? No problem Hierarchical JSON-like store,easyto develop app Source Forge. Yeah. We like FOSS 5. MongoDB to Relational Mental Mapping
Relational (SQL) MongoDB Database Database Table Collection Index Index Row Document Column Field 6.
7. PyMongo: Getting Started
8. PyMongo: Insert / Update / Delete
9. PyMongo: Queries, Indexes
10. PyMongo: Aggregation et.al.
11. PyMongo: GridFS >>>import gridfs >>>fs= gridfs.GridFS(db) >>>withfs .new_file()asfp: ...fp .write( 'The file') ...>>>fp >>>fp ._id ObjectId('4e727f64eb03300c0b000003') >>>fs .get(fp._id).read() 'The file'
12. PyMongo: GridFS Versioning >>> file_id =fs .put( 'Moar data!', filename = 'foo.txt') >>>fs .get_last_version( 'foo.txt') .read() 'Moar data! >>> file_id =fs .put( 'Even moar data!', filename = 'foo.txt') >>>fs .get_last_version( 'foo.txt') .read() 'Even moar data! >>>fs .get_version( 'foo.txt',- 2) .read() 'Moar data! >>>fs .list() [u'foo.txt'] >>>fs .delete(fs.get_last_version( 'foo.txt') ._id) >>>fs .list() [u'foo.txt'] >>>fs .delete(fs.get_last_version( 'foo.txt') ._id) >>>fs .list() [] 13.
14. Why Ming?
15. Ming: Engines & Sessions >>>import ming.datastore >>>ds= ming.datastore.DataStore( 'mongodb://localhost:27017', database = 'test') >>>ds .db Database(Connection('localhost', 27017), u'test') >>>session= ming.Session(ds) >>>session .db Database(Connection('localhost', 27017), u'test') >>>ming .configure(**{ 'ming.main.master':'mongodb://localhost:27017', 'ming.main.database':'test'}) >>>Session .by_name( 'main') .db Database(Connection(u'localhost', 27017), u'test') 16. Ming: Define Your Schema
17. Ming: Define Your Schema Once more, withfeeling
18. Ming: Use Your Schema
19. Ming: Adding Your own Types
class ForceInt (ming .schema.FancySchemaItem): def _validate( self, value): try :returnint(value) exceptTypeError: raiseInvalid( 'Bad value%s '% value, value,None) 20. Ming Bonus: Mongo-in-Memory >>>ming .datastore.DataStore( 'mim://', database = 'test') .db mim.Database(test)
21.
22. Ming ORM: Classes and Collectionsfrom ming importschema, Field from ming.orm import(mapper, Mapper, RelationProperty,ForeignIdProperty) WikiDoc=collection( wiki_page' , session, Field( '_id' , schema . ObjectId()), Field( 'title' ,str , index = True ), Field( 'text' ,str )) CommentDoc=collection( comment' , session, Field( '_id' , schema . ObjectId()), Field( 'page_id' , schema . ObjectId(), index = True ), Field( 'text' ,str )) class WikiPage ( object ):pass class Comment ( object ):pass ormsession . mapper(WikiPage, WikiDoc, properties = dict ( comments = RelationProperty( 'WikiComment' ))) ormsession . mapper(Comment, CommentDoc, properties = dict ( page_id = ForeignIdProperty( 'WikiPage' ), page = RelationProperty( 'WikiPage' ))) Mapper . compile_all() 23. Ming ORM: Classes and Collections (declarative)class WikiPage (MappedClass): class __mongometa__ : session= main_orm_session name= 'wiki_page indexes= ['title' ] _id =FieldProperty(S.ObjectId) title = FieldProperty( str) text= FieldProperty( str) class CommentDoc (MappedClass): class __mongometa__ : session= main_orm_session name= 'comment indexes= ['page_id' ] _id =FieldProperty(S.ObjectId) page_id = ForeignIdProperty(WikiPage) page = RelationProperty(WikiPage) text = FieldProperty( str) 24. Ming ORM: Sessions and Queries
>>>pg= WikiPage(title= 'MyPage', text = 'is here') >>>session .db.wiki_page.count() 0 >>>main_orm_session .flush() >>>session .db.wiki_page.count() 1 25. Ming ORM: Extending the Session
26.
27. Tips From the Trenches
28. Future Work
29. Related Projects Ming http://sf.net/projects/merciless/ MIT License Zarkov http://sf.net/p/zarkov/ Apache License Allura http://sf.net/p/allura/ Apache License PyMongo http://api.mongodb.org/python Apache License 30. Rick Copeland @rick446 [email_address]