Stalled project due mongodb sucks now. Python object wrapper for MongoDB. Project have not any with monogodb-object - its complete rewrited.
Switch branches/tags
Nothing to show
Pull request Compare This branch is 17 commits ahead of marcboeker:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

MongoDB object wrapper for python


mongomodels is a cocktail of the Django ORM mixed with JavaScript dot object notation. Every document is returned as object that can be traversed using the objects attributes. It is based on the awesome pymongo lib by mike from mongodb.

A simple JSON document

    'me': {'age': 24}, 
    'name': 'john', 
    'friends': ['mike', 'dwight', 'elliot']

can now be accessed like this

>>> print
>>> print doc.friends[0]
>>> print

For those who are familar with the Django ORM, mongodb-object behaves very similar.

Getting started


git clone git://
cd mongodb-object
sudo python install

Do the mambo

Let's start with a simple demonstration in the Python interactive console. Open a MongoDB connection and start with a mongodb-object collection. The collection is the base container for our work with mongodb-object.

>>> import pymongo
>>> from mongomodels import Model, Manager

>>> db = pymongo.Connection().test_db
>>> class User(Model):
>>> manager = Manager(db, User)

Now we create a new document using an empty document template from the collection. This is necessary because the document needs to know to which collection it belongs

>>> doc = User(name='John')
>>> = 'john'
>>> doc.person = {}
>>> doc.person.age = 24
>>> doc.person.gender = 'male'
>>> doc
User({'person': {'gender': 'male', 'age': 24}, '_id': ObjectId('4bbeccd863a9f01309000000'), 'name': 'John'})

We can also reference documents on the fly. Retrieval is powered by lazy loading. Only when you select an attribute of a referenced document, the document will be retrieved.

>>> doc2 = User()
>>> doc2.friends = [doc]
>>> doc2.friends
[DBRef('User', ObjectId('4bbeccd863a9f01309000000'))]
>>> doc2.friends[0].get().person.age

Finding documents

As a collection holds all documents, we have to ask the collection when we want to find a special document.

>>> manager.find_one(person__age=24)
User({u'person': {u'gender': u'male', u'age': 24}, u'_id': ObjectId('4bbeccd863a9f01309000000'), u'name': u'John'})
>>> manager.find_one(person__age__lt=25)
User({u'person': {u'gender': u'male', u'age': 24}, u'_id': ObjectId('4bbeccd863a9f01309000000'), u'name': u'John'})
>>> manager.find_one(person__age__lt=24)
>>> manager.find_one(person__age=24, name='john')
User({u'person': {u'gender': u'male', u'age': 24}, u'_id': ObjectId('4bbeccd863a9f01309000000'), u'name': u'John'})

As you can see, we emulate the dot notation of embedded documents using the __ chars like Django does. We can also append query operators ($lt, $lte, $gt, $gte, $ne, $nin, $in, $all, $size) at the end.

You cannot only query for one document, you can retrieve also a list of documents using find() instead of find_one(). find(), query() and all() return lazy Query object, and query doesnot actually act on db.

>>> query = manager.find()
>>> query
<mongomodels.query.Query object at 0x8893e6c>
>>> query.count()
>>> for doc in query:
...     print doc, doc.keys()
User({u'person': {u'gender': u'male', u'age': 24}, u'_id': ObjectId('4bbeccd863a9f01309000000'), u'name': u'John'}) [u'person', u'_id', u'name']
User({u'_id': ObjectId('4bbecd3863a9f01309000001'), u'friends': [DBRef(u'User', ObjectId('4bbeccd863a9f01309000000'))]}) [u'_id', u'friends']

find() accepts the same parameters as find_one() does.

Bulk update and remove

To do a bulk update or remove, we use the query() method.

>>> manager.find(name='John').update(set__name='jack')
>>> manager.find_one(name='jack').name
>>> manager.find(name='jack').remove()
>>> manager.find().count()

update() makes use of the MongoDB update operators ($set, $inc, $push, $pushAll, $pull, $pullAll). You have to prefix the document key with one of the operators above. Embedded docs can be accessed using the previously mentiond __ chars. For example:


Document list options (count, skip, limit, sort)

Using the find() method, you get a list of documents. This list can be sorted, or paginated using the known methods from pymongo.

>>> manager.find(name='jack').count()
>>> manager.find().skip(1).limit(1).next()

>>> for doc in manager.find().sort(friends=1):
...    print doc

And now sort on another key.

>>> for doc in manager.find().sort(name=1):
...    print doc

Document operations

A single document can be modified and afterwards saved without any restrictions. You can also delete the document, return its keys or get a dict represntation.

>>> manager.find_one(name='jack').keys()
['name', 'person']
>>> doc = manager.find_one(name='jack')
>>> = 'harry'
>>> manager.find_one(name='harry').name
>>> manager.delete(doc)
>>> manager.find_one(name='jack')