Query Objects

class ommongo.query.Query(type, session, exclude_subclasses=False)

A query object has all of the methods necessary to programmatically generate a mongo query as well as methods to retrieve results of the query or do an update based on it.

In general a query object should be created via Session.query, not directly.

Parameters:
  • type – A subclass of class:ommongo.document.Document
  • db – The Session which this query is associated with.
  • exclude_subclasses – If this is set to false (the default) and type is polymorphic then subclasses are also retrieved.
add_to_set(qfield, value)

Refer to: add_to_set()

aggregate(raw_query)
all()

Return all of the results of a query in a list

append(qfield, value)

Refer to: append()

ascending(qfield)

Sort the result based on qfield in ascending order. These calls can be chained to sort by multiple fields.

Parameters:qfield – Instance of :class:ommongo.query.QueryField specifying which field to sort by.
clone()

Creates a clone of the current query and all settings. Further updates to the cloned object or the original object will not affect each other

count(with_limit_and_skip=False)

Execute a count on the number of results this query would return.

Parameters:with_limit_and_skip – Include .limit() and .skip() arguments in the count?
descending(qfield)

Sort the result based on qfield in ascending order. These calls can be chained to sort by multiple fields.

Parameters:qfield – Instance of :class:ommongo.query.QueryField specifying which field to sort by.
distinct(key)

Execute this query and return all of the unique values of key.

Parameters:key – the instance of ommongo.QueryField to use as the distinct key.
explain()

Executes an explain operation on the database for the current query and returns the raw explain object returned.

extend(qfield, *value)

Refer to: extend()

fields(*fields)

Only return the specified fields from the object. Accessing a field that was not specified in fields will result in a :class:ommongo.document.FieldNotRetrieved exception being raised

Parameters:fields – Instances of :class:ommongo.query.QueryField specifying which fields to return
filter(*query_expressions)

Apply the given query expressions to this query object

Example: s.query(SomeObj).filter(SomeObj.age > 10, SomeObj.blood_type == 'O')

Parameters:query_expressions – Instances of ommongo.query_expression.QueryExpression

See also

QueryExpression class

filter_by(**filters)

Filter for the names in filters being equal to the associated values. Cannot be used for sub-objects since keys must be strings

filter_dict(query, **kwargs)

Filter for DictField().

Examples: query.filter_dict({"User.Fullname": "Oji"})

filter_like(**filters)

Filter query using re.compile().

Examples: query.filter_like(Name="andi")

find_and_modify(new=False, remove=False)

The mongo “find and modify” command. Behaves like an update expression in that “execute” must be called to do the update and return the results.

Parameters:
  • new – Whether to return the new object or old (default: False)
  • remove – Whether to remove the object before returning it
first()

Execute the query and return the first result. Unlike one, if there are multiple documents it simply returns the first one. If there are no documents, first returns None

hint_asc(qfield)

Applies a hint for the query that it should use a (qfield, ASCENDING) index when performing the query.

Parameters:qfield – the instance of ommongo.QueryField to use as the key.
hint_desc(qfield)

Applies a hint for the query that it should use a (qfield, DESCENDING) index when performing the query.

Parameters:qfield – the instance of ommongo.QueryField to use as the key.
in_(qfield, *values)

Check to see that the value of qfield is one of values

Parameters:
inc(*args, **kwargs)

Refer to: inc()

limit(limit)

Sets the limit on the number of documents returned

Parameters:limit – the number of documents to return
map_reduce(mapper, reducer, key, query)
nin(qfield, *values)

Check to see that the value of qfield is not one of values

Parameters:
not_(*query_expressions)

Add a $not expression to the query, negating the query expressions given.

Examples: query.not_(SomeDocClass.age <= 18) becomes {'age' : { '$not' : { '$gt' : 18 } }}

Parameters:query_expressions – Instances of ommongo.query_expression.QueryExpression
one()

Execute the query and return one result. If more than one result is returned, raises a BadResultException

or_(first_qe, *qes)

Add a $not expression to the query, negating the query expressions given. The | operator on query expressions does the same thing

Examples: query.or_(SomeDocClass.age == 18, SomeDocClass.age == 17) becomes {'$or' : [{ 'age' : 18 }, { 'age' : 17 }]}

Parameters:query_expressions – Instances of ommongo.query_expression.QueryExpression
pop_first(qfield)

Refer to: pop_first()

pop_last(qfield)

Refer to: pop_last()

query

The mongo query object which would be executed if this Query object were used

query_bypass(query, raw_output=True)

Bypass query meaning that field check and validation is skipped, then query object directly executed by pymongo.

Parameters:raw_output – Skip OmMongo ORM layer (default: True)
raw_output()

Turns on raw output, meaning that the OmMongo ORM layer is skipped and the results from pymongo are returned. Useful if you want to use the query functionality without getting python objects back

remove(qfield, value)

Refer to: remove()

remove_all(qfield, *value)

Refer to: remove_all()

search(value, createIndex=None)

Full-text support, make sure that text index already exist on collection. Raise IndexNotFound if text index not exist.

Examples: query.search('pecel lele', createIndex=['FullName', 'Username'])

set(*args, **kwargs)

Refer to: set()

skip(skip)

Sets the number of documents to skip in the result

Parameters:skip – the number of documents to skip
sort(*sort_tuples)

pymongo-style sorting. Accepts a list of tuples.

Parameters:sort_tuples – varargs of sort tuples.
unset(qfield)

Refer to: unset()

class ommongo.query.QueryResult(session, cursor, type, raw_output=False, fields=None)
clone()
next()
rewind()