1. Vladimir Sidorenko
  2. Mongolore




Mongolore is an ORM-like wrapper for MongoDB.

It is heavily based on python-couchdb mapping module by Christopher Lenz. I just did some adjustments for MongoDB and validation support.


  1. Schema declaration Including complex nested objects.

  2. Validation Custom validators support. Validation is only done on object save. Inspired by WTForms.

  3. Auto-refs Dereference on first access only.

  4. Embedded objects (without dbrefs) To reuse repeated pattern in your objects. Look at example below.

  5. Django-like managers Without changes they are just wrappers around pytmongo.Collection, but they allow you to encapsulate your own complex queries.

  6. Small codebase Thin wrapper around pymongo. Just 5 files, most fit one screen.

Simple usage:

from mongolore import *
connection = Connection()
db = Database(connection, 'mongolore_test_db')

# for a complete list of validators and fields see validators.py and mapping.py
class Player(Document):
    name = TextField(validators=[Required()])
    contact = DictField(Mapping.build(
        email = TextField(validators=[Required()]),
        addr = TextField(),
        phones = ListField(TextField(), validators=[Length(max=2)]),

p = Player(name='mongo')
p.contact.email = 'test@gmail.com'
p.contact.addr = 'address'
p.contact.phones = ['first', 'second']

p = Player.objects.find_one()

Inline validators:

class Player(Document):
    name = TextField(validators=[Required()])

    def validate_name(self, value):
        if not value == 'mongo':
            raise ValidationError()

Embedded objects with dbref:

    class A(Document):
        name = TextField()

    class B(Document):
        name = TextField()
        embedded = DBRefField(A)

    a = A(name='mongoa')

    b = B(name='mongob', embedded=a)
    assert isinstance(b.embedded, A)
    b = B.objects.find_one()
    # note that DBRef is dereferenced on first access only
    assert isinstance(b.embedded, A)

Embedded objects without dbref:

    class Contact(Mapping):
        email = TextField()
        addr = TextField()

        def validate_email(self, value):

        def __unicode__(self):

    class Player(Document):
        name = TextField()
        # contact is just and embedded dictionary
        # useful for encapsulation and reusability
        contact = DictField(Contact)

    p = Player(name='12')
    # you can set contact using Contact instance
    p.contact = Contact(email='', addr='')
    # or dictionary
    p.contact = {'email': '', 'addr': ''}
    # result is the same
    assert isinstance(p.contact, Contact)

Managers and queriyng database:

    # you can use document's manager
    # manager is just a wrapper around pymongo.collection, syntax is the same
    Player.objects.find({'name': 'mongo'})

    # you can also use original pymongo interface, objects will be converted
    # document is saved to collection named after classname
    db.player.filter() #.insert() ...

    # you can define your managers too
    class CustomManager(Manager):
        def startswith(self, s):
            return self.find({'name': {'$regex': r'^%s' % s}})

    class Player(Document):
        name = TextField()

        custom = CustomManager()

    # 'objects' manager is always created
    assert hasattr(Player, 'objects')
    p = Player(name='mongodb')
    assert p.custom.startswith('mongo').count() == 1