0
Inside PyMongo

Mike Dirolf (@mdirolf)
PyMongo
(see also: MongoKit, Ming, MongoEngine, etc.)
API Basics
>>> from pymongo import Connection

>>> db = Connection().test_db

>>> db.test.insert({"x": 1})
ObjectId('4bdaf...
In 1.6
GridFS (setup)

>>>   from pymongo import Connection
>>>   import gridfs
>>>
>>>   db = Connection().gridfs_example
>>>   ...
GridFS (before)
>>>   f = fs.open("hello.txt", "w")
>>>   f.write("hello ")
>>>   f.write("world")
>>>   f.close()

>>> g ...
GridFS (after)

>>> fs.put(“hello world”)

>>> file_id = fs.put("hello world")
>>> fs.get(file_id).read()
'hello world'
GridFS (after)
>>> myfile = fs.new_file(location=[-74, 40.74])
>>> myfile.write("hello ")
>>> myfile.write("world,")
>>> m...
Commands (before)

>>> db.command({“buildinfo”: 1})

>>> db.command({“collstats”: collection})

>>> db.command(SON([(“file...
Commands (after)

>>> db.command(“buildinfo”)

>>> db.command(“collstats”, collection)

>>> db.command(“filemd5”, object_i...
PyMongo + mod_wsgi
Stored JS (1.5)

>>> db.system_js.add1 = "function (x) { return x + 1; }"

>>> db.system_js.add1(5)
6.0

>>> del db.system...
In 1.6+
$slice and
           Field Negation

>>> db.test.find(fields=[“foo”])

>>> db.test.find(fields={“foo”: 0})

>>> db.test.f...
y2038
Aware Datetimes
max_scan


>>> db.test.find({“x”: 1}).max_scan(25)
as_class
>>> c = Connection()
>>> c.db.test.save({"x": 1})
ObjectId('4bf6a71be6fb1b4bce000000')

>>> c.db.test.find_one()
...
Get involved!
http://github.com/mongodb/mongo-python-driver
?
Yes...
 ...but also sometimes no
Similar to                            +
• A lot of Django doesn’t depend on django.db:
 • URL dispatch, templates, I18N, c...
settings.py
DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_...
Representing a Poll

{'question': 'Do MongoDB + Django <3 each other?',
 'pub_date': datetime.datetime(2010, 1, 21),
 'cho...
models.py (PyMongo)
def save_poll(question):
  return db.polls.insert({"question": question,
                  "pub_date":...
models.py (MongoKit)

class Poll(mongokit.Document):
   structure = {"question": str,
            "pub_date": datetime,
  ...
models.py (Ming)
class Poll(ming.Document):

  class __mongometa__:
     session = session
     name = "polls"

  _id = mi...
mango - sessions and auth


•   Full sessions support
•   mango provided User class
    •   supports is_authenticated(), s...
mango - sessions and auth


SESSION_ENGINE = 'mango.session'
AUTHENTICATION_BACKENDS = ('mango.auth.Backend',)
MONGODB_HOS...
What about admin?

• No great solution... yet.
• Could replace admin app like mango does
  for sessions / auth
• Or...
Supporting MongoDB
    in django.db
Upcoming SlideShare
Loading in...5
×

Inside PyMongo - MongoNYC

7,031

Published on

Inside PyMongo: a talk about PyMongo internals (sort of a "what's new in PyMongo") given at MongoNYC

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,031
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
106
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide































  • Transcript of "Inside PyMongo - MongoNYC"

    1. 1. Inside PyMongo Mike Dirolf (@mdirolf)
    2. 2. PyMongo (see also: MongoKit, Ming, MongoEngine, etc.)
    3. 3. API Basics >>> from pymongo import Connection >>> db = Connection().test_db >>> db.test.insert({"x": 1}) ObjectId('4bdafd07e6fb1b351e000000') >>> db.test.find_one() {u'x': 1, u'_id': ObjectId('4bdafd07e6fb1b351e000000') }
    4. 4. In 1.6
    5. 5. GridFS (setup) >>> from pymongo import Connection >>> import gridfs >>> >>> db = Connection().gridfs_example >>> fs = gridfs.GridFS(db)
    6. 6. GridFS (before) >>> f = fs.open("hello.txt", "w") >>> f.write("hello ") >>> f.write("world") >>> f.close() >>> g = fs.open("hello.txt") >>> g.read() 'hello world' >>> g.close()
    7. 7. GridFS (after) >>> fs.put(“hello world”) >>> file_id = fs.put("hello world") >>> fs.get(file_id).read() 'hello world'
    8. 8. GridFS (after) >>> myfile = fs.new_file(location=[-74, 40.74]) >>> myfile.write("hello ") >>> myfile.write("world,") >>> myfile.writelines([" and have a ", "good day!"]) >>> myfile.close() >>> out = fs.get(myfile._id) >>> out.read() 'hello world, and have a good day!' >>> out.location [-74, 40.740000000000002]
    9. 9. Commands (before) >>> db.command({“buildinfo”: 1}) >>> db.command({“collstats”: collection}) >>> db.command(SON([(“filemd5”, object_id), (“root”, file_root)]))
    10. 10. Commands (after) >>> db.command(“buildinfo”) >>> db.command(“collstats”, collection) >>> db.command(“filemd5”, object_id, root=file_root)
    11. 11. PyMongo + mod_wsgi
    12. 12. Stored JS (1.5) >>> db.system_js.add1 = "function (x) { return x + 1; }" >>> db.system_js.add1(5) 6.0 >>> del db.system_js.add1 http://dirolf.com/2010/04/05/stored-javascript-in-mongodb-and-pymongo.html
    13. 13. In 1.6+
    14. 14. $slice and Field Negation >>> db.test.find(fields=[“foo”]) >>> db.test.find(fields={“foo”: 0}) >>> db.test.find(fields={“foo”: {“$slice”: -2}})
    15. 15. y2038
    16. 16. Aware Datetimes
    17. 17. max_scan >>> db.test.find({“x”: 1}).max_scan(25)
    18. 18. as_class >>> c = Connection() >>> c.db.test.save({"x": 1}) ObjectId('4bf6a71be6fb1b4bce000000') >>> c.db.test.find_one() {u'x': 1, u'_id': ObjectId('4bf6a71be6fb1b4bce000000')} >>> c.db.test.find_one(as_class=SON) SON([(u'_id', ObjectId('4bf6a71be6fb1b4bce000000')), (u'x', 1)]) >>> c.document_class=SON >>> c.db.test.find_one() SON([(u'_id', ObjectId('4bf6a71be6fb1b4bce000000')), (u'x', 1)])
    19. 19. Get involved! http://github.com/mongodb/mongo-python-driver
    20. 20. ?
    21. 21. Yes... ...but also sometimes no
    22. 22. 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
    23. 23. 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', )
    24. 24. 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...'}]}
    25. 25. 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
    26. 26. 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
    27. 27. 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/
    28. 28. mango - sessions and auth • Full sessions support • mango provided User class • supports is_authenticated(), set_password(), etc. http://github.com/vpulim/mango
    29. 29. 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
    30. 30. What about admin? • No great solution... yet. • Could replace admin app like mango does for sessions / auth • Or...
    31. 31. Supporting MongoDB in django.db
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×