Mongo Query Expression Language¶
Query Fields¶
-
class
ommongo.query_expression.
QueryField
(type, parent=None)¶ -
elem_match
(value)¶ This method does two things depending on the context:
- In the context of a query expression it:
Creates a query expression to do an $elemMatch on the selected field. If the type of this field is a DocumentField the value can be either a QueryExpression using that Document’s fields OR you can use a dict for raw mongo.
See the mongo documentation for thorough treatment of elemMatch: http://docs.mongodb.org/manual/reference/operator/elemMatch/
- In the context of choosing fields in a query.fields() expr:
Sets the field to use elemMatch, so only the matching elements of a list are used. See the mongo docs for more details: http://docs.mongodb.org/manual/reference/projection/elemMatch/
-
endswith
(suffix, ignore_case=False, options=None)¶ A query to check if a field ends with a given suffix string
Example:
session.query(Spell).filter(Spells.name.endswith("cadabra", ignore_case=True))
-
eq_
(value)¶ Creates a query expression where
this field == value
Note
The prefered usage is via an operator:
User.name == value
-
exclude
()¶ Use in a query.fields() expression to say this field should be excluded. The default of fields() is to include only fields which are specified. This allows retrieving of “every field except ‘foo’”.
-
exists
(exists=True)¶ Create a MongoDB query to check if a field exists on a Document.
-
fields_expression
¶
-
ge_
(value)¶ Creates a query expression where
this field >= value
Note
The prefered usage is via an operator:
User.name >= value
-
get_absolute_name
()¶ Returns the full dotted name of this field
-
get_type
()¶ Returns the underlying
ommongo.fields.Field
-
gt_
(value)¶ Creates a query expression where
this field > value
Note
The prefered usage is via an operator:
User.name > value
-
in_
(*values)¶ A query to check if this query field is one of the values in
values
. Produces a MongoDB$in
expression.
-
le_
(value)¶ Creates a query expression where
this field <= value
Note
The prefered usage is via an operator:
User.name <= value
-
lt_
(value)¶ Creates a query expression where
this field < value
Note
The prefered usage is via an operator:
User.name < value
-
matched_index
()¶ Represents the matched array index on a query with objects inside of a list. In the MongoDB docs, this is the
$
operator
-
ne_
(value)¶ Creates a query expression where
this field != value
Note
The prefered usage is via an operator:
User.name != value
-
near
(x, y, max_distance=None)¶ Return documents near the given point
-
near_sphere
(x, y, max_distance=None)¶ Return documents near the given point using sphere distances
-
nin
(*values)¶ A query to check if this query field is not one of the values in
values
. Produces a MongoDB$nin
expression.
-
regex
(expression, ignore_case=False, options=None)¶ A query to check if a field matches a given regular expression :param ignore_case: Whether or not to ignore the case (setting this to True is the same as setting the ‘i’ option) :param options: A string of option characters, as per the MongoDB $regex operator (e.g. “imxs”)
Example:
session.query(Spell).filter(Spells.name.regex(r'^abra[a-z]*cadabra$', ignore_case=True))
-
startswith
(prefix, ignore_case=False, options=None)¶ A query to check if a field starts with a given prefix string
Example:
session.query(Spell).filter(Spells.name.startswith("abra", ignore_case=True))
Note
This is a shortcut to .regex(‘^’ + re.escape(prefix)) MongoDB optimises such prefix expressions to use indexes appropriately. As the prefix contains no further regex, this will be optimized by matching only against the prefix.
-
within_box
(corner1, corner2)¶ Adapted from the Mongo docs:
session.query(Places).filter(Places.loc.within_box(cornerA, cornerB)
-
within_polygon
(polygon)¶ Adapted from the Mongo docs:
polygonA = [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ] polygonB = { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } } session.query(Places).filter(Places.loc.within_polygon(polygonA) session.query(Places).filter(Places.loc.within_polygon(polygonB)
-
within_radius
(x, y, radius)¶ Adapted from the Mongo docs:
session.query(Places).filter(Places.loc.within_radius(1, 2, 50)
-
within_radius_sphere
(x, y, radius)¶ Adapted from the Mongo docs:
session.query(Places).filter(Places.loc.within_radius_sphere(1, 2, 50)
-
Query Expressions¶
-
class
ommongo.query_expression.
QueryExpression
(obj)¶ A QueryExpression wraps a dictionary representing a query to perform on a mongo collection. The
Note
There is no
and_
expression because multiple expressions can be specified to a single call ofQuery.filter()
-
not_
()¶ Negates this instance’s query expression using MongoDB’s
$not
operatorExample:
(User.name == 'Jeff').not_()
Note
Another usage is via an operator, but parens are needed to get past precedence issues:
~ (User.name == 'Jeff')
-
or_
(expression)¶ Adds the given expression to this instance’s MongoDB
$or
expression, starting a new one if one does not exstExample:
(User.name == 'Jeff').or_(User.name == 'Jack')
Note
The prefered usageis via an operator:
User.name == 'Jeff' | User.name == 'Jack'
-