python and mongodb

Download Python and MongoDB

If you can't read please download the document

Upload: christiano-anderson

Post on 16-Apr-2017

24.399 views

Category:

Technology


0 download

TRANSCRIPT

Python and MongoDB

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

Distinctdb.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 functionsfunction () {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 postSecond PostThird 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='[email protected]', 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 Djangohttp://miud.in/fHE

Contribute

MongoDB Users Grouphttp://groups.google.com/group/mongodb-user

MongoDB in Spanishhttp://www.mongodb.org/display/DOCSES/Inicio

#MongoDB at irc.freenode.net

MongoSpanish Bloghttp://mongospanish.blogspot.com/

Gracias!!! - Questions?

Christiano [email protected]

Twitter: @dumpBlog: http://christiano.me

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso