Skip to content

eguven/nanomongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ca50233 · Apr 3, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 3, 2018
Apr 6, 2013
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018
Apr 2, 2018

Repository files navigation

nanomongo

https://travis-ci.org/eguven/nanomongo.png

nanomongo is a minimal MongoDB Object-Document Mapper for Python. It does not attempt to be a feature-complete ODM but if you enjoy using PyMongo API with dictionaries and often find yourself writing validators and pymongo.Collection wrappers, nanomongo might suit your needs.

Quick Links: Source (github) - Documentation (rtd) - Packages (PyPi) - Changelog

Quickstart

import pymongo
from nanomongo import Field, BaseDocument

client = pymongo.MongoClient()

# python3 notation, see documentation for python2 options
# we can omit the keyword arguments here and later call MyDoc.register(client=client, db='dbname')
class MyDoc(BaseDocument, dot_notation=True, client=client, db='dbname'):
    foo = Field(str)
    bar = Field(int, required=False)

    __indexes__ = [
        pymongo.IndexModel('foo'),
        pymongo.IndexModel([('bar', 1), ('foo', -1)], unique=True),
    ]

doc = MyDoc(foo='L33t')  # creates document {'foo': 'L33t'}
doc.insert()             # inserts document {'_id': ObjectId('...'), 'foo': 'L33t'}
doc.bar = 42             # records the change
doc.save()               # calls collection.update_one {'$set': {'bar': 42}}

MyDoc.find_one({'foo': 'L33t'})
{'_id': ObjectId('...'), 'bar': 42, 'foo': 'L33t'}
Author:Eren Güven (GitHub, Twitter)
License:Apache License 2.0