odmantic.query
odmantic.query.QueryExpression
¶
Base object used to build queries.
All comparison and logical operators returns QueryExpression
objects.
The |
and &
operators are supported for respectively the
or and the and logical operators.
Warning
When using those operators make sure to correctly bracket the expressions to avoid python operator precedence issues.
Logical Operators¶
odmantic.query.and_(*elements)
¶
Logical AND operation between multiple QueryExpression
objects.
Source code in odmantic/query.py
def and_(*elements: QueryDictBool) -> QueryExpression:
"""Logical **AND** operation between multiple `QueryExpression` objects."""
return QueryExpression({"$and": elements})
odmantic.query.or_(*elements)
¶
Logical OR operation between multiple QueryExpression
objects.
Source code in odmantic/query.py
def or_(*elements: QueryDictBool) -> QueryExpression:
"""Logical **OR** operation between multiple `QueryExpression` objects."""
return QueryExpression({"$or": elements})
odmantic.query.nor_(*elements)
¶
Logical NOR operation between multiple QueryExpression
objects.
Source code in odmantic/query.py
def nor_(*elements: QueryDictBool) -> QueryExpression:
"""Logical **NOR** operation between multiple `QueryExpression` objects."""
return QueryExpression({"$nor": elements})
Comparison Operators¶
odmantic.query.eq(field, value)
¶
Equality comparison operator.
Source code in odmantic/query.py
def eq(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Equality comparison operator."""
return _cmp_expression(field, "$eq", value)
odmantic.query.ne(field, value)
¶
Inequality comparison operator (includes documents not containing the field).
Source code in odmantic/query.py
def ne(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Inequality comparison operator (includes documents not containing the field)."""
return _cmp_expression(field, "$ne", value)
odmantic.query.gt(field, value)
¶
Greater than (strict) comparison operator (i.e. >).
Source code in odmantic/query.py
def gt(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Greater than (strict) comparison operator (i.e. >)."""
return _cmp_expression(field, "$gt", value)
odmantic.query.gte(field, value)
¶
Greater than or equal comparison operator (i.e. >=).
Source code in odmantic/query.py
def gte(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Greater than or equal comparison operator (i.e. >=)."""
return _cmp_expression(field, "$gte", value)
odmantic.query.lt(field, value)
¶
Less than (strict) comparison operator (i.e. <).
Source code in odmantic/query.py
def lt(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Less than (strict) comparison operator (i.e. <)."""
return _cmp_expression(field, "$lt", value)
odmantic.query.lte(field, value)
¶
Less than or equal comparison operator (i.e. <=).
Source code in odmantic/query.py
def lte(field: FieldProxyAny, value: Any) -> QueryExpression:
"""Less than or equal comparison operator (i.e. <=)."""
return _cmp_expression(field, "$lte", value)
odmantic.query.in_(field, sequence)
¶
Select instances where field
is contained in sequence
.
Source code in odmantic/query.py
def in_(field: FieldProxyAny, sequence: Sequence) -> QueryExpression:
"""Select instances where `field` is contained in `sequence`."""
return _cmp_expression(field, "$in", sequence)
odmantic.query.not_in(field, sequence)
¶
Select instances where field
is not contained in sequence
.
Source code in odmantic/query.py
def not_in(field: FieldProxyAny, sequence: Sequence) -> QueryExpression:
"""Select instances where `field` is **not** contained in `sequence`."""
return _cmp_expression(field, "$nin", sequence)
odmantic.query.match(field, pattern)
¶
Select instances where field
matches the pattern
regular expression.
Source code in odmantic/query.py
def match(field: FieldProxyAny, pattern: Union[Pattern, str]) -> QueryExpression:
"""Select instances where `field` matches the `pattern` regular expression."""
# FIXME might create incompatibilities
# https://docs.mongodb.com/manual/reference/operator/query/regex/#regex-and-not
if isinstance(pattern, str):
r = re.compile(pattern)
else:
r = pattern
return QueryExpression({+field: r})
Sort helpers¶
odmantic.query.SortExpression
¶
Base object used to build sort queries.
odmantic.query.asc(field)
¶
Sort by ascending field
.
Source code in odmantic/query.py
def asc(field: FieldProxyAny) -> SortExpression:
"""Sort by ascending `field`."""
return _build_sort_expression(field, 1)
odmantic.query.desc(field)
¶
Sort by descending field
.
Source code in odmantic/query.py
def desc(field: FieldProxyAny) -> SortExpression:
"""Sort by descending `field`."""
return _build_sort_expression(field, -1)