SlideShare a Scribd company logo
1 of 28
Rick Copeland @rick446
Arborian Consulting, LLC
http://www.flickr.com/photos/fazen/9079179/
-   Get started with PyMongo


-   Sprinkle in some Ming schemas


-   ODM: When a dict just won’t do
>>>importpymongo
>>>conn = pymongo.Connection()
>>>conn
Connection('localhost', 27017)
>>>conn.test
Database(Connection('localhost', 27017), u'test')
>>>conn.test.foo
Collection(Database(Connection('localhost', 27017), u'test'), u'foo')
>>>conn['test-db']
Database(Connection('localhost', 27017), u'test-db')
>>>conn['test-db']['foo-collection']
Collection(Database(Connection('localhost', 27017), u'test-db'), u'foo-
  collection')
>>>conn.test.foo.bar.baz
Collection(Database(Connection('localhost', 27017), u'test'), u'foo.bar.baz')
>>>db = conn.test
>>>id = db.foo.insert({'bar':1, 'baz':[ 1, 2, {'k':5} ] })
>>>id
ObjectId('4e712e21eb033009fa000000')                          Auto-Generated _id
>>>db.foo.find()
<pymongo.cursor.Cursor object at 0x29c7d50>
>>>list(db.foo.find())                                             Cursors are python
[{u'bar': 1, u'_id': ObjectId('4e712e21eb033009fa000000'), u'baz': [1, 2, {'k': 5}]}]
                                                                       generators
>>>db.foo.update({'_id':id}, {'$set': { 'bar':2}})
>>>db.foo.find().next()
{u'bar': 2, u'_id': ObjectId('4e712e21eb033009fa000000'), u'baz': [1, 2, {'k': 5}]}
>>>db.foo.remove({'_id':id})
>>>list(db.foo.find())
[]
                                                             Remove uses same
                                                           query language as find()
>>>db.foo.insert([ dict(x=x) forxinrange(10) ])
[ObjectId('4e71313aeb033009fa00000b'), … ]
>>>list(db.foo.find({ 'x': {'$gt': 3} }))                                    Range Query
[{u'x': 4, u'_id': ObjectId('4e71313aeb033009fa00000f')}, {u'x': 5, u'_id':
     ObjectId('4e71313aeb033009fa000010')}, {u'x': 6, u'_id':
     ObjectId('4e71313aeb033009fa000011')}, …]
>>>list(db.foo.find({ 'x': {'$gt': 3} }, { '_id':0 } ))
[{u'x': 4}, {u'x': 5}, {u'x': 6}, {u'x': 7}, {u'x': 8}, {u'x': 9}]
>>>list(db.foo.find({ 'x': {'$gt': 3} }, { '_id':0 } )
....skip(1).limit(2))
[{u'x': 5}, {u'x': 6}]
>>>db.foo.ensure_index([
                                                                   Partial Retrieval
...('x’,pymongo.ASCENDING),('y’,pymongo.DESCENDING)])
u'x_1_y_-1’


                                                      Compound Indexes
One Rule (for now): Avoid Javascript

                                       http://www.flickr.com/photos/lizjones/295567490/
   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.
>>>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.
>>>file_id = fs.put('Moar data!', filename='foo.txt')
>>>fs.get_last_version('foo.txt').read()
'Moar data!’
                                                                         Create file by
                                                                           filename
>>>file_id = fs.put('Evenmoar 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()
                                                            “2nd from the last”
[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()
[]
-   Get started with PyMongo


-   Sprinkle in some Ming schemas


-   ODM: When a dict just won’t do
   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
Model              Datastore
(schema)             (database)



           Session
>>>importming.datastore
>>>ds = ming.datastore.DataStore('mongodb://localhost:27017', database='test')
>>>ds.db                                                         Connection +
Database(Connection('localhost', 27017), u'test')                 Database
>>>session = ming.Session(ds)
>>>session.db
Database(Connection('localhost', 27017), u'test')
>>>ming.configure(**{                                          Optimized for config
... 'ming.main.master':'mongodb://localhost:27017',                   files
... 'ming.main.database':'test'})
>>>Session.by_name('main').db
Database(Connection(u'localhost', 27017), u'test')
http://www.flickr.com/photos/pictureclara/5333266789/
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
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
>>>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'])
>>>ming.datastore.DataStore('mim://', database='test').db
mim.Database(test)



   MongoDB is (generally) fast
       … except when creating databases
       … particularly when you preallocate


   Unit tests like things to be isolated


   MIM gives you isolation at the expense of speed & scaling
-   Get started with PyMongo


-   Sprinkle in some Ming schemas


-   ODM: When a dict just won’t do
frommingimport schema, Field
fromming.odmimport (mapper, Mapper, RelationProperty,
ForeignIdProperty)
                                                          Plain Old Python
WikiDoc= collection('wiki_page',session, … )                   Classes
CommentDoc= collection(’comment’, session, … )

classWikiPage(object): pass                               Map classes to
classComment(object): pass                              collection + session
odmsession.mapper(WikiPage, WikiDoc, properties=dict(
comments=RelationProperty('WikiComment')))
odmsession.mapper(Comment, CommentDoc, properties=dict(
page_id=ForeignIdProperty('WikiPage'),
page=RelationProperty('WikiPage')))

                                                               “Relations”
classWikiPage(MappedClass):
class__mongometa__:
session = main_odm_session
name='wiki_page’
indexes = [ 'title']

    _id = FieldProperty(S.ObjectId)
title = FieldProperty(str)
text = FieldProperty(str)
comments = RelationProperty(’Comment’)
       Session ODMSession
       My_collection.m… My_mapped_class.query…
       ODMSessionactually does stuff
           Track object identity
           Track object modifications
           Unit of work flushing all changes at once
    >>>pg = WikiPage(title='MyPage', text='is here')
    >>>session.db.wiki_page.count()
    0
    >>>main_orm_session.flush()
    >>>session.db.wiki_page.count()
    1
http://www.flickr.com/photos/39747297@N05/5229733647/
   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
   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
PyMongo
http://api.mongodb.org/python
Apache License



Ming
http://sf.net/projects/merciless/
MIT License
Rick Copeland @rick446
   Arborian Consulting, LLC




Feedback? http://www.surveymonkey.com/s/5DLCYKN

                              http://www.flickr.com/photos/f-oxymoron/5005673112/

More Related Content

What's hot

From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
Jaime Buelta
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and Creativity
MongoDB
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query Optimization
MongoDB
 

What's hot (20)

Patterns for slick database applications
Patterns for slick database applicationsPatterns for slick database applications
Patterns for slick database applications
 
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202
 
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebBDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
 
Drupal 8 database api
Drupal 8 database apiDrupal 8 database api
Drupal 8 database api
 
The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.7 book - Part 32 of 196
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
Mythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDBMythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDB
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210
 
Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)Indexing and Query Optimizer (Mongo Austin)
Indexing and Query Optimizer (Mongo Austin)
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDB
 
The Ring programming language version 1.2 book - Part 32 of 84
The Ring programming language version 1.2 book - Part 32 of 84The Ring programming language version 1.2 book - Part 32 of 84
The Ring programming language version 1.2 book - Part 32 of 84
 
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and Creativity
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The Browser
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query Optimization
 
The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.1 book - Part 27 of 180
 
Using Scala Slick at FortyTwo
Using Scala Slick at FortyTwoUsing Scala Slick at FortyTwo
Using Scala Slick at FortyTwo
 
MongoDB With Style
MongoDB With StyleMongoDB With Style
MongoDB With Style
 

Similar to Rapid and Scalable Development with MongoDB, PyMongo, and Ming

Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)
MongoSF
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
kchodorow
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 

Similar to Rapid and Scalable Development with MongoDB, PyMongo, and Ming (20)

Inside PyMongo - MongoNYC
Inside PyMongo - MongoNYCInside PyMongo - MongoNYC
Inside PyMongo - MongoNYC
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Python Development (MongoSF)
Python Development (MongoSF)Python Development (MongoSF)
Python Development (MongoSF)
 
Latinoware
LatinowareLatinoware
Latinoware
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)
 
Web Components With Rails
Web Components With RailsWeb Components With Rails
Web Components With Rails
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorial
 
Protractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applicationsProtractor framework – how to make stable e2e tests for Angular applications
Protractor framework – how to make stable e2e tests for Angular applications
 
Practical Google App Engine Applications In Py
Practical Google App Engine Applications In PyPractical Google App Engine Applications In Py
Practical Google App Engine Applications In Py
 
Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17Hopping in clouds - phpuk 17
Hopping in clouds - phpuk 17
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
 
Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Starting with MongoDB
Starting with MongoDBStarting with MongoDB
Starting with MongoDB
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
 

More from Rick Copeland

Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.io
Rick Copeland
 

More from Rick Copeland (12)

Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Schema Design at Scale
Schema Design at ScaleSchema Design at Scale
Schema Design at Scale
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
 
Chef on MongoDB and Pyramid
Chef on MongoDB and PyramidChef on MongoDB and Pyramid
Chef on MongoDB and Pyramid
 
Scaling with MongoDB
Scaling with MongoDBScaling with MongoDB
Scaling with MongoDB
 
Chef on Python and MongoDB
Chef on Python and MongoDBChef on Python and MongoDB
Chef on Python and MongoDB
 
Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.io
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
 
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQRealtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
Realtime Analytics Using MongoDB, Python, Gevent, and ZeroMQ
 
Rapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and PythonRapid, Scalable Web Development with MongoDB, Ming, and Python
Rapid, Scalable Web Development with MongoDB, Ming, and Python
 
Allura - an Open Source MongoDB Based Document Oriented SourceForge
Allura - an Open Source MongoDB Based Document Oriented SourceForgeAllura - an Open Source MongoDB Based Document Oriented SourceForge
Allura - an Open Source MongoDB Based Document Oriented SourceForge
 
MongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBMongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDB
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Rapid and Scalable Development with MongoDB, PyMongo, and Ming

  • 3. - Get started with PyMongo - Sprinkle in some Ming schemas - ODM: When a dict just won’t do
  • 4. >>>importpymongo >>>conn = pymongo.Connection() >>>conn Connection('localhost', 27017) >>>conn.test Database(Connection('localhost', 27017), u'test') >>>conn.test.foo Collection(Database(Connection('localhost', 27017), u'test'), u'foo') >>>conn['test-db'] Database(Connection('localhost', 27017), u'test-db') >>>conn['test-db']['foo-collection'] Collection(Database(Connection('localhost', 27017), u'test-db'), u'foo- collection') >>>conn.test.foo.bar.baz Collection(Database(Connection('localhost', 27017), u'test'), u'foo.bar.baz')
  • 5. >>>db = conn.test >>>id = db.foo.insert({'bar':1, 'baz':[ 1, 2, {'k':5} ] }) >>>id ObjectId('4e712e21eb033009fa000000') Auto-Generated _id >>>db.foo.find() <pymongo.cursor.Cursor object at 0x29c7d50> >>>list(db.foo.find()) Cursors are python [{u'bar': 1, u'_id': ObjectId('4e712e21eb033009fa000000'), u'baz': [1, 2, {'k': 5}]}] generators >>>db.foo.update({'_id':id}, {'$set': { 'bar':2}}) >>>db.foo.find().next() {u'bar': 2, u'_id': ObjectId('4e712e21eb033009fa000000'), u'baz': [1, 2, {'k': 5}]} >>>db.foo.remove({'_id':id}) >>>list(db.foo.find()) [] Remove uses same query language as find()
  • 6. >>>db.foo.insert([ dict(x=x) forxinrange(10) ]) [ObjectId('4e71313aeb033009fa00000b'), … ] >>>list(db.foo.find({ 'x': {'$gt': 3} })) Range Query [{u'x': 4, u'_id': ObjectId('4e71313aeb033009fa00000f')}, {u'x': 5, u'_id': ObjectId('4e71313aeb033009fa000010')}, {u'x': 6, u'_id': ObjectId('4e71313aeb033009fa000011')}, …] >>>list(db.foo.find({ 'x': {'$gt': 3} }, { '_id':0 } )) [{u'x': 4}, {u'x': 5}, {u'x': 6}, {u'x': 7}, {u'x': 8}, {u'x': 9}] >>>list(db.foo.find({ 'x': {'$gt': 3} }, { '_id':0 } ) ....skip(1).limit(2)) [{u'x': 5}, {u'x': 6}] >>>db.foo.ensure_index([ Partial Retrieval ...('x’,pymongo.ASCENDING),('y’,pymongo.DESCENDING)]) u'x_1_y_-1’ Compound Indexes
  • 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.
  • 10. >>>file_id = fs.put('Moar data!', filename='foo.txt') >>>fs.get_last_version('foo.txt').read() 'Moar data!’ Create file by filename >>>file_id = fs.put('Evenmoar 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() “2nd from the last” [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() []
  • 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
  • 13. Model Datastore (schema) (database) Session
  • 14. >>>importming.datastore >>>ds = ming.datastore.DataStore('mongodb://localhost:27017', database='test') >>>ds.db Connection + Database(Connection('localhost', 27017), u'test') Database >>>session = ming.Session(ds) >>>session.db Database(Connection('localhost', 27017), u'test') >>>ming.configure(**{ Optimized for config ... 'ming.main.master':'mongodb://localhost:27017', files ... 'ming.main.database':'test'}) >>>Session.by_name('main').db Database(Connection(u'localhost', 27017), u'test')
  • 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'])
  • 19. >>>ming.datastore.DataStore('mim://', database='test').db mim.Database(test)  MongoDB is (generally) fast  … except when creating databases  … particularly when you preallocate  Unit tests like things to be isolated  MIM gives you isolation at the expense of speed & scaling
  • 20. - Get started with PyMongo - Sprinkle in some Ming schemas - ODM: When a dict just won’t do
  • 21. frommingimport schema, Field fromming.odmimport (mapper, Mapper, RelationProperty, ForeignIdProperty) Plain Old Python WikiDoc= collection('wiki_page',session, … ) Classes CommentDoc= collection(’comment’, session, … ) classWikiPage(object): pass Map classes to classComment(object): pass collection + session odmsession.mapper(WikiPage, WikiDoc, properties=dict( comments=RelationProperty('WikiComment'))) odmsession.mapper(Comment, CommentDoc, properties=dict( page_id=ForeignIdProperty('WikiPage'), page=RelationProperty('WikiPage'))) “Relations”
  • 22. classWikiPage(MappedClass): class__mongometa__: session = main_odm_session name='wiki_page’ indexes = [ 'title'] _id = FieldProperty(S.ObjectId) title = FieldProperty(str) text = FieldProperty(str) comments = RelationProperty(’Comment’)
  • 23. Session ODMSession  My_collection.m… My_mapped_class.query…  ODMSessionactually does stuff  Track object identity  Track object modifications  Unit of work flushing all changes at once >>>pg = WikiPage(title='MyPage', text='is here') >>>session.db.wiki_page.count() 0 >>>main_orm_session.flush() >>>session.db.wiki_page.count() 1
  • 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

  1. 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!
  2. Someone is going to put weird data in your database.Or they will try.Catch them in the act.