Skip to content

Commit

Permalink
Add more operators
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Sep 27, 2023
1 parent 4f6fca2 commit 51e5433
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 1 addition & 4 deletions mongoz/core/db/querysets/core/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
VALUE_EQUALITY = ["eq", "neq", "contains", "icontains", "where", "pattern"]
LIST_EQUALITY = ["in", "not_in"]
ORDER_EQUALITY = ["asc", "desc"]
GREATNESS_EQUALITY = ["lt", "lte", "gt", "gte"]

ASCENDING = "asc"
DESCENDING = "desc"
GREATNESS_EQUALITY = ["lt", "lte", "gt", "gte", "exists"]
2 changes: 1 addition & 1 deletion mongoz/core/db/querysets/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ async def distinct_values(self, key: str) -> List[Any]:
"""
manager: "Manager" = self.clone()
filter_query = Expression.compile_many(manager._filter)
values = await self._collection.find(filter_query).distinct(key=key)
values = await manager._collection.find(filter_query).distinct(key=key)
return cast(List[Any], values)

async def where(self, condition: Union[str, Code]) -> Any:
Expand Down
20 changes: 20 additions & 0 deletions mongoz/core/db/querysets/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def lte(cls, key: Any, value: Union[bool, Expression]) -> Expression:
return Expression(key=key, operator=ExpressionOperator.LESS_THAN_EQUAL, value=value)


class Element:
"""
All element query operators.
"""

@classmethod
def exists(cls, key: Any, value: Union[bool, Expression]) -> Expression:
return Expression(key=key, operator=ExpressionOperator.EXISTS, value=value)


class Q(Ordering, Iterable, Equality, Comparison):
"""
Shortcut for the creation of an Expression.
Expand All @@ -112,3 +122,13 @@ def and_(cls, *args: Union[bool, Expression]) -> Expression:
def or_(cls, *args: Union[bool, Expression]) -> Expression:
assert not isinstance(args, bool) # type: ignore
return Expression(key=ExpressionOperator.OR, operator=ExpressionOperator.OR, value=args)

@classmethod
def nor(cls, *args: Union[bool, Expression]) -> Expression:
assert not isinstance(args, bool) # type: ignore
return Expression(key=ExpressionOperator.NOR, operator=ExpressionOperator.NOR, value=args)

@classmethod
def not_(cls, *args: Union[bool, Expression]) -> Expression:
assert not isinstance(args, bool) # type: ignore
return Expression(key=ExpressionOperator.NOT, operator=ExpressionOperator.NOT, value=args)
3 changes: 3 additions & 0 deletions mongoz/utils/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ class ExpressionOperator(str, Enum):
LESS_THAN = "$lt"
AND = "$and"
OR = "$or"
NOR = "$nor"
NOT = "$not"
EXISTS = "$exists"

0 comments on commit 51e5433

Please sign in to comment.