• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Python and MongoDB
 

Python and MongoDB

on

  • 18,530 views

Presentation about Python and MongoDB

Presentation about Python and MongoDB

Statistics

Views

Total Views
18,530
Views on SlideShare
15,586
Embed Views
2,944

Actions

Likes
32
Downloads
393
Comments
1

17 Embeds 2,944

http://blog.nosqlfan.com 2466
http://simple-is-better.com 384
http://christiano.me 36
http://www.lifeyun.com 28
http://xue.uplook.cn 6
http://www.uplook.cn 5
http://xianguo.com 4
http://paper.li 3
http://www.elearning.vsons.com 3
http://www.simple-is-better.com 2
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
http://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

11 of 1 previous next

  • 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 Python and MongoDB Presentation Transcript

    • Python and MongoDB
    • Christiano Anderson
      • 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
    • What is MongoDB?
      • MongoDB is...
        • Open-Source,
        • High-Performance,
        • Schema-Free,
        • Document-Oriented
        • Database
    • 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).”
    • MongoDB in production
    • Schema-Free
      • Good at:
        • Dynamically typed languages;
        • Migrations;
        • Flexibility;
        • Web;
        • Caching;
      • Not good at:
        • Transactional datas;
        • If you require full SQL support;
    • Document
      • A document in MongoDB is like a JSON.
      • Example:
        • {'name': 'Christiano',
        • 'language': 'Python',
        • 'country': 'Brazil'}
    • Migrations?
      • You can dinamically update your data, example:
        • {'name': 'Christiano',
        • 'language': 'Python',
        • 'country': 'Brazil'}
        • {'name': 'Christiano',
        • 'language': 'Python',
        • 'country': 'Brazil',
        • 'event': 'PyConAr'}
    • Aggregation
      • Distinct
        • db.programmers.insert({”language”:”python”})
        • db.programmers.insert({”language”:”perl”})
        • db.programmers.insert({”language”:”ruby”})
        • db.programmers.insert({”language”:”python”})
        • db.programmers.distinct(”language”)
    • Map/Reduce
      • Use JavaScript to create Map/Reduce functions
      • function () {
        • this.tags.forEach(function(z)) {
          • emit (z, 1);
        • });}
        • Function (key, value) {
          • var total = 0;
          • for (var i = 0; i < values.length; i++) {
            • total += values[i]; }
          • return total;
        • }
    • SQL to Mongo
    • More SQL to Mongo
    • Other cool features
      • Full index support;
      • GridFS;
      • Replication & High Availability;
      • Auto-Sharding;
      • Free software;
      • But also provides commercial support;
    • Using MongoDB with Python
      • PyMongo is the Python driver for MongoDB;
      • 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
        • $ cd pymongo/
        • $ python setup.py install
    • Connecting
        >>> from pymongo import Connection >>> con = Connection('localhost') >>> db = con['blog']
    • 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')
    • 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')]
    • 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'})
    • 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
    • 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'}
    • Operators
      • You can use operators, like $ne, $lte, $gte, and many others. See more at:
        • http://www.mongodb.org/display/DOCS/Advanced+Queries
    • MongoEngine
      • An object-document mapper to connect to Python and MongoDB
      • Similar to Django ORM
        • $ easy_install mongoengine
    • MongoEngine example
        class User(Document): email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50)
    • 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()
    • Adding data
      • u = User(email='anderson@trianguli.net', first_name='Christiano', last_name='Anderson')
      • u.save()
    • Accessing data
      • for u in User.objects:
        • print u.email
    • Django and MongoDB
      • Yes, it is possible, but...
      • There is no great solution for:
        • Auth;
        • Sessions;
        • Admin;
        • Other ORM issues;
    • Django and MongoDB
      • You can use MongoEngine to create model-like schema;
      • MongoEngine provides:
        • Authentication Backend;
        • Sessions Backend;
      • Works fine, but it is not a native Django solution (yet)
    • Django and MongoEngine Auth
      • Settings.py:
      • from mongoengine import *
      • connect('testblog')
        • AUTHENTICATION_BACKENDS = (
        • 'mongoengine.django.auth.MongoEngineBackend',
        • )
    • Django and MongoDB Sessions
        SESSION_ENGINE = 'mongoengine.django.sessions'
    • Try out!
      • Projects:
        • http://www.mongodb.org
        • http://mongoengine.org
      • Great article about MongoDB and Django
        • http://miud.in/fHE
    • 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
      • MongoSpanish Blog
        • http://mongospanish.blogspot.com/
    • Gracias!!! - Questions? Christiano Anderson [email_address] Twitter: @dump Blog: http://christiano.me