| 1 |
import re |
| 2 |
from abc import ABC |
| 3 |
|
| 4 |
from beanie.odm.operators.find import BaseFindOperator |
| 5 |
|
| 6 |
|
| 7 |
class BaseFindEvaluationOperator(BaseFindOperator, ABC): ... |
| 8 |
|
| 9 |
|
| 10 |
class Expr(BaseFindEvaluationOperator): |
| 11 |
""" |
| 12 |
`$type` query operator |
| 13 |
|
| 14 |
Example: |
| 15 |
|
| 16 |
```python |
| 17 |
class Sample(Document): |
| 18 |
one: int |
| 19 |
two: int |
| 20 |
|
| 21 |
Expr({"$gt": [ "$one" , "$two" ]}) |
| 22 |
``` |
| 23 |
|
| 24 |
Will return query object like |
| 25 |
|
| 26 |
```python |
| 27 |
{"$expr": {"$gt": [ "$one" , "$two" ]}} |
| 28 |
``` |
| 29 |
|
| 30 |
MongoDB doc: |
| 31 |
<https://docs.mongodb.com/manual/reference/operator/query/expr/> |
| 32 |
""" |
| 33 |
|
| 34 |
def __init__(self, expression: dict): |
| 35 |
self.expression = expression |
| 36 |
|
| 37 |
@property |
| 38 |
def query(self): |
| 39 |
return {"$expr": self.expression} |
| 40 |
|
| 41 |
|
| 42 |
class JsonSchema(BaseFindEvaluationOperator): |
| 43 |
""" |
| 44 |
`$jsonSchema` query operator |
| 45 |
|
| 46 |
MongoDB doc: |
| 47 |
<https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/> |
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, expression: dict): |
| 51 |
self.expression = expression |
| 52 |
|
| 53 |
@property |
| 54 |
def query(self): |
| 55 |
return {"$jsonSchema": self.expression} |
| 56 |
|
| 57 |
|
| 58 |
class Mod(BaseFindEvaluationOperator): |
| 59 |
""" |
| 60 |
`$mod` query operator |
| 61 |
|
| 62 |
Example: |
| 63 |
|
| 64 |
```python |
| 65 |
class Sample(Document): |
| 66 |
one: int |
| 67 |
|
| 68 |
Mod(Sample.one, 4, 0) |
| 69 |
``` |
| 70 |
|
| 71 |
Will return query object like |
| 72 |
|
| 73 |
```python |
| 74 |
{ "one": { "$mod": [ 4, 0 ] } } |
| 75 |
``` |
| 76 |
|
| 77 |
MongoDB doc: |
| 78 |
<https://docs.mongodb.com/manual/reference/operator/query/mod/> |
| 79 |
""" |
| 80 |
|
| 81 |
def __init__(self, field, divisor: int, remainder: int): |
| 82 |
self.field = field |
| 83 |
self.divisor = divisor |
| 84 |
self.remainder = remainder |
| 85 |
|
| 86 |
@property |
| 87 |
def query(self): |
| 88 |
return {self.field: {"$mod": [self.divisor, self.remainder]}} |
| 89 |
|
| 90 |
|
| 91 |
class RegEx(BaseFindEvaluationOperator): |
| 92 |
""" |
| 93 |
`$regex` query operator |
| 94 |
|
| 95 |
MongoDB doc: |
| 96 |
<https://docs.mongodb.com/manual/reference/operator/query/regex/> |
| 97 |
""" |
| 98 |
|
| 99 |
def __init__( |
| 100 |
self, |
| 101 |
field, |
| 102 |
pattern: str | re.Pattern[bytes] | re.Pattern[str], |
| 103 |
options: str | None = None, |
| 104 |
): |
| 105 |
self.field = field |
| 106 |
self.pattern = pattern |
| 107 |
self.options = options |
| 108 |
|
| 109 |
@property |
| 110 |
def query(self): |
| 111 |
expression = {"$regex": self.pattern} |
| 112 |
if self.options: |
| 113 |
expression["$options"] = self.options |
| 114 |
return {self.field: expression} |
| 115 |
|
| 116 |
|
| 117 |
class Text(BaseFindEvaluationOperator): |
| 118 |
""" |
| 119 |
`$text` query operator |
| 120 |
|
| 121 |
Example: |
| 122 |
|
| 123 |
```python |
| 124 |
class Sample(Document): |
| 125 |
description: Indexed(str, pymongo.TEXT) |
| 126 |
|
| 127 |
Text("coffee") |
| 128 |
``` |
| 129 |
|
| 130 |
Will return query object like |
| 131 |
|
| 132 |
```python |
| 133 |
{ |
| 134 |
"$text": { |
| 135 |
"$search": "coffee" , |
| 136 |
"$caseSensitive": False, |
| 137 |
"$diacriticSensitive": False |
| 138 |
} |
| 139 |
} |
| 140 |
``` |
| 141 |
|
| 142 |
MongoDB doc: |
| 143 |
<https://docs.mongodb.com/manual/reference/operator/query/text/> |
| 144 |
|
| 145 |
Note: if you need to run a query against Azure Cosmos DB for MongoDB, |
| 146 |
which does not support diacritic sensitivity yet, you can set |
| 147 |
`diacritic_sensitive` argument to `None` to exclude it from the query. |
| 148 |
""" |
| 149 |
|
| 150 |
def __init__( |
| 151 |
self, |
| 152 |
search: str, |
| 153 |
language: str | None = None, |
| 154 |
case_sensitive: bool = False, |
| 155 |
diacritic_sensitive: bool | None = False, |
| 156 |
): |
| 157 |
""" |
| 158 |
|
| 159 |
:param search: str |
| 160 |
:param language: Optional[str] = None |
| 161 |
:param case_sensitive: bool = False |
| 162 |
:param diacritic_sensitive: Optional[bool] = False |
| 163 |
""" |
| 164 |
self.search = search |
| 165 |
self.language = language |
| 166 |
self.case_sensitive = case_sensitive |
| 167 |
self.diacritic_sensitive = diacritic_sensitive |
| 168 |
|
| 169 |
@property |
| 170 |
def query(self): |
| 171 |
expression = { |
| 172 |
"$text": { |
| 173 |
"$search": self.search, |
| 174 |
"$caseSensitive": self.case_sensitive, |
| 175 |
} |
| 176 |
} |
| 177 |
if self.language: |
| 178 |
expression["$text"]["$language"] = self.language |
| 179 |
if self.diacritic_sensitive is not None: |
| 180 |
expression["$text"]["$diacriticSensitive"] = ( |
| 181 |
self.diacritic_sensitive |
| 182 |
) |
| 183 |
return expression |
| 184 |
|
| 185 |
|
| 186 |
class Where(BaseFindEvaluationOperator): |
| 187 |
""" |
| 188 |
`$where` query operator |
| 189 |
|
| 190 |
MongoDB doc: |
| 191 |
<https://docs.mongodb.com/manual/reference/operator/query/where/> |
| 192 |
""" |
| 193 |
|
| 194 |
def __init__(self, expression: str): |
| 195 |
self.expression = expression |
| 196 |
|
| 197 |
@property |
| 198 |
def query(self): |
| 199 |
return {"$where": self.expression} |