Skip to content Skip to sidebar Skip to footer

Mongodb Orm For Python?

I am trying to migrate from sqlalchemy(SQlite) to using mongodb. I would like schema vertification. I amm looking at mongokit, but I want something which is similar to mappers, s

Solution 1:

Another option is MongoEngine. The ORM for MongoEngine is very similar to the ORM used by Django.

Example (from the tutorial):

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()

Solution 2:

Not being satisfied with either MongoKit or MongoEngine, I decided to write my own object-oriented interface for Python.

I delegated all queries directly to pymongo, so the query syntax there is the same. Mostly, it's just an object-wrapper around the results, with some other helpers like database connection pooling, DBRef support, and other convenience methods to make your life easier.

It's called Minimongo and it's available from github. Happy hacking!

Example:

from minimongo import Model, MongoCollection 

classMyObject(Model): 
    model = MongoCollection(database='test', collection='my_collection')

m = MyObject()
m.x = 1
m.field = 'value'
m.other = {'list': True}
m.save()

x = MyObject({'x': 1, 'y': 2}).save()

objs = MyObject.find({'x': 1})
for o in objs: 
    print o

Solution 3:

You want MongoKit. It is one layer of abstraction higher than PyMongo. Not sure if you're using Django, but there's also django-mongokit integration.

Example from this blog post. Note that instances of Computer can then reference make/model directly once the structure is defined ( e.g. atari.make, c64.model, ... ). No need for dictionaries:

import datetime 
from mongokit import Document

classComputer(Document):

    structure = { 
      'make': unicode, 
      'model': unicode, 
      'purchase_date': datetime.datetime, 
      'cpu_ghz': float, 
    }

    validators = { 
      'cpu_ghz': lambda x: x > 0, 
      'make': lambda x: x.strip(), 
    }

    default_values = { 
      'purchase_date': datetime.datetime.utcnow, 
    }

    use_dot_notation = True

    indexes = [ 
      {'fields': ['make']}, 
    ]

Solution 4:

I know I'm really late to this question, but I'm the author of Ming http://merciless.sourceforge.net, a SQLAlchemy-inspired MongoDB validation and ORM engine. It's what we use at SourceForge, and there's a reasonable presentation available at http://www.slideshare.net/rick446/rapid-and-scalable-development-with-mongodb-pymongo-and-ming as well as a case study on migrating from SQLAlchemy to Ming http://www.slideshare.net/__amol__/from-sqlalchemy-to-ming-with-turbogears2. Here's an example of the ORM layer in Ming (from the tutorial):

classWikiPage(MappedClass):

    class__mongometa__:
        session = session
        name = 'wiki_page'

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(str)
    text = FieldProperty(str)
    comments=RelationProperty('WikiComment')

Queries use the standard MongoDB query syntax (not Django ORM's magic keyword arguments):

WikiComment.query.find(dict(page_id=wp._id))

Post a Comment for "Mongodb Orm For Python?"