Your SlideShare is downloading. ×
Python and MongoDB
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Python and MongoDB

19,372
views

Published on

Presentation about Python and MongoDB

Presentation about Python and MongoDB

Published in: Technology

1 Comment
34 Likes
Statistics
Notes
No Downloads
Views
Total Views
19,372
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
454
Comments
1
Likes
34
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

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...
  • 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:
    • 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:
  • 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