Collections

Collections provide a quick and easy way to interact with many documents of the same type all at once. They also provide a mechanism for basic joins across one addition table (due to current limitations of RethinkDB and how it handles joins). Collections act like python lists of RethinkModel objects, but provide an easy interface to order, filter, and limit the results.

General Usage

Collections are pretty simplistic objects. Just instantiate a new object with the model which you are constructing the collection from.

collection = RethinkCollection(gateModel)

Note

Optionally you can also pass a dictionary which will be used as a filter. For more information on how filters work, please see the RethinkDB docs

Order the Results

collection.orderBy("episodes", "ASC")

Limit the Results

collection.limit(10, 5) # Limit to 10 results, starting after the 5th one.

Fetch the Results

result = collection.fetch()

result acts like a bit like a normal python list, containing all of the documents which are part of the collection, all pre-wrapped in a RethinkModel object.

RethinkCollection

class rethinkORM.RethinkCollection(model, filter=None, query=None, conn=None)

A way to fetch groupings of documents that meet a criteria and have them in an iterable storage object, with each document represented by RethinkModel objects

RethinkCollection.__init__(model, filter=None, query=None, conn=None)

Instantiates a new collection, using the given models table, and wrapping all documents with the given model.

Filter can be a dictionary of keys to filter by, a lambda or a similar statement, see: RethinkDB Filters for more information

A pre built query can also be passed in, to allow for better control of what documents get included in the final collection.

Parameters:
  • model – A RethinkModel object to be used to wrap all documents in
  • filter – If provided, it will be passed using the ReQL .filter command
  • query – An optional pre built ReQL query to be used
RethinkCollection.order_by(key, direction='desc')

Allows for the results to be ordered by a specific field. If given, direction can be set with passing an additional argument in the form of “asc” or “desc”

Warning

In pre v1.0.0 releases, this was named orderBy

Parameters:
  • key – The key to sort by
  • direction – The direction, DESC or ASC to sort by
RethinkCollection.limit(limit, offset=0)

Limits the results to the given offset (0 if not given) and the stated limit.

Parameters:
  • limit – The number of documents that the results should be limited to
  • offset – The number of documents to skip
RethinkCollection.fetch()

Fetches the query results and wraps the documents in the collections model.

Documents can then be accessed through the standard collection[index] or with a for loop:

for doc in collection:
    pass