Python and MongoDB
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Python and MongoDB

on

  • 19,834 views

Presentation about Python and MongoDB

Presentation about Python and MongoDB

Statistics

Views

Total Views
19,834
Views on SlideShare
16,771
Embed Views
3,063

Actions

Likes
33
Downloads
428
Comments
1

18 Embeds 3,063

http://blog.nosqlfan.com 2575
http://simple-is-better.com 388
http://christiano.me 36
http://www.lifeyun.com 28
http://xue.uplook.cn 9
http://www.uplook.cn 6
http://www.elearning.vsons.com 4
http://xianguo.com 4
http://paper.li 3
http://www.simple-is-better.com 2
http://www.linkedin.com 1
http://192.168.6.52 1
http://cache.baidu.com 1
http://hlblog.com 1
url_unknown 1
http://reader.youdao.com 1
http://www.slideshare.net 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • wow
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Python and MongoDB Presentation Transcript

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