Python and MongoDB
Christiano Anderson <ul><li>Works with free software since 1995;
Former system engineer of Terra Networks Brazil;
Former GNU Free Software Directory maintainer;
Python developer since 2000, member of ApyB;
Member of MongoDB pt_BR translation team;
Founder of Trianguli free software;
Free Software and NoSQL evangelist;
3/263 </li></ul>
What is MongoDB? <ul><li>MongoDB is... </li><ul><li>Open-Source,
High-Performance,
Schema-Free,
Document-Oriented
Database </li></ul></ul>
What is MongoDB? ”MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RD...
MongoDB in production
Schema-Free <ul><li>Good at: </li><ul><li>Dynamically typed languages;
Migrations;
Flexibility;
Web;
Caching; </li></ul><li>Not good at: </li><ul><li>Transactional datas;
If you require full SQL support; </li></ul></ul>
Document <ul><li>A document in MongoDB is like a JSON.
Example: </li><ul><li>{'name': 'Christiano',
'language': 'Python',
'country': 'Brazil'} </li></ul></ul>
Migrations? <ul><li>You can dinamically update your data, example: </li><ul><li>{'name': 'Christiano',
'language': 'Python',
'country': 'Brazil'}
{'name': 'Christiano',
'language': 'Python',
'country': 'Brazil',
'event': 'PyConAr'} </li></ul></ul>
Upcoming SlideShare
Loading in...5
×

Python and MongoDB

19,949

Published on

Presentation about Python and MongoDB

Published in: Technology
1 Comment
36 Likes
Statistics
Notes
No Downloads
Views
Total Views
19,949
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
469
Comments
1
Likes
36
Embeds 0
No embeds

No notes for slide

Python and MongoDB

  1. 1. Python and MongoDB
  2. 2. Christiano Anderson <ul><li>Works with free software since 1995;
  3. 3. Former system engineer of Terra Networks Brazil;
  4. 4. Former GNU Free Software Directory maintainer;
  5. 5. Python developer since 2000, member of ApyB;
  6. 6. Member of MongoDB pt_BR translation team;
  7. 7. Founder of Trianguli free software;
  8. 8. Free Software and NoSQL evangelist;
  9. 9. 3/263 </li></ul>
  10. 10. What is MongoDB? <ul><li>MongoDB is... </li><ul><li>Open-Source,
  11. 11. High-Performance,
  12. 12. Schema-Free,
  13. 13. Document-Oriented
  14. 14. Database </li></ul></ul>
  15. 15. What is MongoDB? ”MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide rich queries and deep functionality).”
  16. 16. MongoDB in production
  17. 17. Schema-Free <ul><li>Good at: </li><ul><li>Dynamically typed languages;
  18. 18. Migrations;
  19. 19. Flexibility;
  20. 20. Web;
  21. 21. Caching; </li></ul><li>Not good at: </li><ul><li>Transactional datas;
  22. 22. If you require full SQL support; </li></ul></ul>
  23. 23. Document <ul><li>A document in MongoDB is like a JSON.
  24. 24. Example: </li><ul><li>{'name': 'Christiano',
  25. 25. 'language': 'Python',
  26. 26. 'country': 'Brazil'} </li></ul></ul>
  27. 27. Migrations? <ul><li>You can dinamically update your data, example: </li><ul><li>{'name': 'Christiano',
  28. 28. 'language': 'Python',
  29. 29. 'country': 'Brazil'}
  30. 30. {'name': 'Christiano',
  31. 31. 'language': 'Python',
  32. 32. 'country': 'Brazil',
  33. 33. 'event': 'PyConAr'} </li></ul></ul>
  34. 34. Aggregation <ul><li>Distinct </li><ul><li>db.programmers.insert({”language”:”python”})
  35. 35. db.programmers.insert({”language”:”perl”})
  36. 36. db.programmers.insert({”language”:”ruby”})
  37. 37. db.programmers.insert({”language”:”python”})
  38. 38. db.programmers.distinct(”language”) </li></ul></ul>
  39. 39. Map/Reduce <ul><li>Use JavaScript to create Map/Reduce functions
  40. 40. function () { </li><ul><li>this.tags.forEach(function(z)) { </li><ul><li>emit (z, 1); </li></ul><li>});}
  41. 41. Function (key, value) { </li><ul><li>var total = 0;
  42. 42. for (var i = 0; i < values.length; i++) { </li><ul><li>total += values[i]; } </li></ul><li>return total; </li></ul><li>} </li></ul></ul>
  43. 43. SQL to Mongo
  44. 44. More SQL to Mongo
  45. 45. Other cool features <ul><li>Full index support;
  46. 46. GridFS;
  47. 47. Replication & High Availability;
  48. 48. Auto-Sharding;
  49. 49. Free software;
  50. 50. But also provides commercial support; </li></ul>
  51. 51. Using MongoDB with Python <ul><li>PyMongo is the Python driver for MongoDB;
  52. 52. You can use setuptools to install: </li><ul><li>$ easy_install pymongo </li></ul><li>Or you can use GitHub: </li><ul><li>$ git clone git://github.com/mongodb/mongo-python-driver.git pymongo
  53. 53. $ cd pymongo/
  54. 54. $ python setup.py install </li></ul></ul>
  55. 55. Connecting <ul>>>> from pymongo import Connection >>> con = Connection('localhost') >>> db = con['blog'] </ul>
  56. 56. Inserting <ul>>>> post = {'title':'My first post', …'author': 'Christiano Anderson', …'content': 'This is my first paragraph', … 'tags': ['mongodb','blog','example']} >>> posts = db['posts'] >>> posts.insert(post) ObjectId('4cb662f508bf532b1b000000') </ul>
  57. 57. Inserting more docs at once <ul>>>> other_posts = [{'title':'Second Post', 'author': 'Christiano Anderson','tags':['test'], 'content': 'Hey, my second Post'}, {'title':'Third Post', 'author':'Luke Skywalker','tags':['naboo'],'content':'Hey princess Leya'}] >>> posts.insert(other_posts) [ObjectId('4cb6651b08bf532b97000000'), ObjectId('4cb6651b08bf532b97000001')] </ul>
  58. 58. Querying one doc <ul>>>> my_post = db.posts.find_one({}) {u'content': u'This is my first post at MongoDB', u'title': u'My first post', u'_id': ObjectId('4cb662f508bf532b1b000000'), u'tags': [u'mongodb', u'blog', u'example'], u'author': u'Christiano Anderson'} >>> my_post = db.posts.find_one({ 'author': 'Christiano Anderson'}) </ul>
  59. 59. Querying an array of docs <ul>>>> all_posts = db.posts.find({}) >>> for p in all_posts: ... print p['title'] My first post Second Post Third Post </ul>
  60. 60. More queries <ul>>>> p = db.posts.find_one({'tags':'naboo'}) {u'content': u'Hey princess Leya', u'title': u'Third Post', u'_id': ObjectId('4cb6651b08bf532b97000001'), u'tags': [u'naboo'], u'author': u'Luke Skywalker'} </ul>
  61. 61. Operators <ul><li>You can use operators, like $ne, $lte, $gte, and many others. See more at: </li><ul><li>http://www.mongodb.org/display/DOCS/Advanced+Queries </li></ul></ul>
  62. 62. MongoEngine <ul><li>An object-document mapper to connect to Python and MongoDB
  63. 63. Similar to Django ORM </li><ul><li>$ easy_install mongoengine </li></ul></ul>
  64. 64. MongoEngine example <ul>class User(Document): email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50) </ul>
  65. 65. MongoEngine example 2 <ul>class Post(Document): title = StringField(max_length=120, required=True) author = ReferenceField(User) class TextPost(Post): content = StringField() class ImagePost(Post): image_path = StringField() class LinkPost(Post): link_url = StringField() </ul>
  66. 66. Adding data <ul><li>u = User(email='anderson@trianguli.net', first_name='Christiano', last_name='Anderson')
  67. 67. u.save() </li></ul>
  68. 68. Accessing data <ul><li>for u in User.objects: </li><ul><li>print u.email </li></ul></ul>
  69. 69. Django and MongoDB <ul><li>Yes, it is possible, but...
  70. 70. There is no great solution for: </li><ul><li>Auth;
  71. 71. Sessions;
  72. 72. Admin;
  73. 73. Other ORM issues; </li></ul></ul>
  74. 74. Django and MongoDB <ul><li>You can use MongoEngine to create model-like schema;
  75. 75. MongoEngine provides: </li><ul><li>Authentication Backend;
  76. 76. Sessions Backend; </li></ul><li>Works fine, but it is not a native Django solution (yet) </li></ul>
  77. 77. Django and MongoEngine Auth <ul><li>Settings.py:
  78. 78. from mongoengine import *
  79. 79. connect('testblog')
  80. 80. … </li><ul><li>AUTHENTICATION_BACKENDS = (
  81. 81. 'mongoengine.django.auth.MongoEngineBackend',
  82. 82. ) </li></ul></ul>
  83. 83. Django and MongoDB Sessions <ul>SESSION_ENGINE = 'mongoengine.django.sessions' </ul>
  84. 84. Try out! <ul><li>Projects: </li><ul><li>http://www.mongodb.org
  85. 85. http://mongoengine.org </li></ul><li>Great article about MongoDB and Django </li><ul><li>http://miud.in/fHE </li></ul></ul>
  86. 86. Contribute <ul><li>MongoDB Users Group </li><ul><li>http://groups.google.com/group/mongodb-user </li></ul><li>MongoDB in Spanish </li><ul><li>http://www.mongodb.org/display/DOCSES/Inicio </li></ul><li>#MongoDB at irc.freenode.net
  87. 87. MongoSpanish Blog </li><ul><li>http://mongospanish.blogspot.com/ </li></ul></ul>
  88. 88. Gracias!!! - Questions? Christiano Anderson [email_address] Twitter: @dump Blog: http://christiano.me
  1. A particular slide catching your eye?

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

×