MongoDB extension

A storage/query backend for MongoDB.

status:beta
database:MongoDB
dependencies:pymongo
suitable for:general purpose (mostly server-side)

Warning

this module is not intended for production. It contains some hacks and should be refactored. However, it is actually used in a real project involving complex queries. Patches, improvements, rewrites are welcome.

class doqu.ext.mongodb.StorageAdapter(**kw)
Parameters:
  • host
  • port
  • database
  • collection
clear()

Clears the whole storage from data.

delete(primary_key)

Permanently deletes the record with given primary key from the database.

get(model, primary_key)

Returns model instance for given model and primary key. Raises KeyError if there is no item with given key in the database.

get_many(doc_class, primary_keys)

Returns a list of documents with primary keys from given list. More efficient than calling get() multiple times.

get_or_create(doc_class, **kwargs)

Queries the database for records associated with given document class and conforming to given extra condtions. If such records exist, picks the first one (the order may be random depending on the database). If there are no such records, creates one.

Returns the document instance and a boolean value “created”.

reconnect()

Gracefully closes current connection (if it’s not broken) and connects again to the database (e.g. reopens the file).

save(data, primary_key=None)

Saves given model instance into the storage. Returns primary key.

Parameters:
  • data – dict containing all properties to be saved
  • primary_key – the key for given object; if undefined, will be generated

Note that you must provide current primary key for a model instance which is already in the database in order to update it instead of copying it.

class doqu.ext.mongodb.QueryAdapter(*args, **kw)
delete()

Deletes all records that match current query.

values(name)

Returns distinct values for given field.

Parameters:
  • name – the field name.

Note

A set is dynamically build on client side if the query contains conditions. If it doesn’t, a much more efficient approach is used. It is only available within current connection, not query.

where(**conditions)

Returns Query instance filtered by given conditions. The conditions are defined exactly as in Pyrant’s high-level query API. See pyrant.query.Query.filter documentation for details.

where_not(**conditions)

Returns Query instance. Inverted version of where().

Previous topic

Tokyo Tyrant extension

Next topic

WTForms extension

This Page