tags:

views:

585

answers:

2

im trying to convert from sqlalchemy (sqlite) to using mongodb. i would like schema vertification. im looking at mongokit, but i want something with similar to mappers, so that it would save from the object's property, and not a dict.

i would like a mapper so that i can use existing objects without modifying them.

+4  A: 

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

class Computer(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']}, 
    ]
Ryan Cox
is there a way to do this without modifying existing business logic objects? in sqlalchemy, you can use mappers.
Timmy
more surgical change. keeps your dependency graph clean. makes sense, though I don't see a way to do this directly. Maybe something strange like class MongoComputer(Computer,Document) or with some form of mix in? Interesting...
Ryan Cox
its clean in sqlalchemy, thus the question, thans
Timmy
+4  A: 

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