| 1 |
from beanie.odm.operators.update import BaseUpdateOperator |
| 2 |
|
| 3 |
|
| 4 |
class BaseUpdateGeneralOperator(BaseUpdateOperator): |
| 5 |
operator = "" |
| 6 |
|
| 7 |
def __init__(self, expression): |
| 8 |
self.expression = expression |
| 9 |
|
| 10 |
@property |
| 11 |
def query(self): |
| 12 |
return {self.operator: self.expression} |
| 13 |
|
| 14 |
|
| 15 |
class Set(BaseUpdateGeneralOperator): |
| 16 |
""" |
| 17 |
`$set` update query operator |
| 18 |
|
| 19 |
Example: |
| 20 |
|
| 21 |
```python |
| 22 |
class Sample(Document): |
| 23 |
one: int |
| 24 |
|
| 25 |
Set({Sample.one: 2}) |
| 26 |
``` |
| 27 |
|
| 28 |
Will return query object like |
| 29 |
|
| 30 |
```python |
| 31 |
{"$set": {"one": 2}} |
| 32 |
``` |
| 33 |
|
| 34 |
MongoDB doc: |
| 35 |
<https://docs.mongodb.com/manual/reference/operator/update/set/> |
| 36 |
""" |
| 37 |
|
| 38 |
operator = "$set" |
| 39 |
|
| 40 |
|
| 41 |
class SetRevisionId: |
| 42 |
""" |
| 43 |
`$set` update query operator |
| 44 |
|
| 45 |
Example: |
| 46 |
|
| 47 |
```python |
| 48 |
class Sample(Document): |
| 49 |
one: int |
| 50 |
|
| 51 |
Set({Sample.one: 2}) |
| 52 |
``` |
| 53 |
|
| 54 |
Will return query object like |
| 55 |
|
| 56 |
```python |
| 57 |
{"$set": {"one": 2}} |
| 58 |
``` |
| 59 |
|
| 60 |
MongoDB doc: |
| 61 |
<https://docs.mongodb.com/manual/reference/operator/update/set/> |
| 62 |
""" |
| 63 |
|
| 64 |
def __init__(self, revision_id): |
| 65 |
self.revision_id = revision_id |
| 66 |
self.operator = "$set" |
| 67 |
self.expression = {"revision_id": self.revision_id} |
| 68 |
|
| 69 |
@property |
| 70 |
def query(self): |
| 71 |
return {self.operator: self.expression} |
| 72 |
|
| 73 |
|
| 74 |
class CurrentDate(BaseUpdateGeneralOperator): |
| 75 |
""" |
| 76 |
`$currentDate` update query operator |
| 77 |
|
| 78 |
Example: |
| 79 |
|
| 80 |
```python |
| 81 |
class Sample(Document): |
| 82 |
ts: datetime |
| 83 |
|
| 84 |
CurrentDate({Sample.ts: True}) |
| 85 |
``` |
| 86 |
|
| 87 |
Will return query object like |
| 88 |
|
| 89 |
```python |
| 90 |
{"$currentDate": {"ts": True}} |
| 91 |
``` |
| 92 |
|
| 93 |
MongoDB doc: |
| 94 |
<https://docs.mongodb.com/manual/reference/operator/update/currentDate/> |
| 95 |
""" |
| 96 |
|
| 97 |
operator = "$currentDate" |
| 98 |
|
| 99 |
|
| 100 |
class Inc(BaseUpdateGeneralOperator): |
| 101 |
""" |
| 102 |
`$inc` update query operator |
| 103 |
|
| 104 |
Example: |
| 105 |
|
| 106 |
```python |
| 107 |
class Sample(Document): |
| 108 |
one: int |
| 109 |
|
| 110 |
Inc({Sample.one: 2}) |
| 111 |
``` |
| 112 |
|
| 113 |
Will return query object like |
| 114 |
|
| 115 |
```python |
| 116 |
{"$inc": {"one": 2}} |
| 117 |
``` |
| 118 |
|
| 119 |
MongoDB doc: |
| 120 |
<https://docs.mongodb.com/manual/reference/operator/update/inc/> |
| 121 |
""" |
| 122 |
|
| 123 |
operator = "$inc" |
| 124 |
|
| 125 |
|
| 126 |
class Min(BaseUpdateGeneralOperator): |
| 127 |
""" |
| 128 |
`$min` update query operator |
| 129 |
|
| 130 |
Example: |
| 131 |
|
| 132 |
```python |
| 133 |
class Sample(Document): |
| 134 |
one: int |
| 135 |
|
| 136 |
Min({Sample.one: 2}) |
| 137 |
``` |
| 138 |
|
| 139 |
Will return query object like |
| 140 |
|
| 141 |
```python |
| 142 |
{"$min": {"one": 2}} |
| 143 |
``` |
| 144 |
|
| 145 |
MongoDB doc: |
| 146 |
<https://docs.mongodb.com/manual/reference/operator/update/min/> |
| 147 |
""" |
| 148 |
|
| 149 |
operator = "$min" |
| 150 |
|
| 151 |
|
| 152 |
class Max(BaseUpdateGeneralOperator): |
| 153 |
""" |
| 154 |
`$max` update query operator |
| 155 |
|
| 156 |
Example: |
| 157 |
|
| 158 |
```python |
| 159 |
class Sample(Document): |
| 160 |
one: int |
| 161 |
|
| 162 |
Max({Sample.one: 2}) |
| 163 |
``` |
| 164 |
|
| 165 |
Will return query object like |
| 166 |
|
| 167 |
```python |
| 168 |
{"$max": {"one": 2}} |
| 169 |
``` |
| 170 |
|
| 171 |
MongoDB doc: |
| 172 |
<https://docs.mongodb.com/manual/reference/operator/update/max/> |
| 173 |
""" |
| 174 |
|
| 175 |
operator = "$max" |
| 176 |
|
| 177 |
|
| 178 |
class Mul(BaseUpdateGeneralOperator): |
| 179 |
""" |
| 180 |
`$mul` update query operator |
| 181 |
|
| 182 |
Example: |
| 183 |
|
| 184 |
```python |
| 185 |
class Sample(Document): |
| 186 |
one: int |
| 187 |
|
| 188 |
Mul({Sample.one: 2}) |
| 189 |
``` |
| 190 |
|
| 191 |
Will return query object like |
| 192 |
|
| 193 |
```python |
| 194 |
{"$mul": {"one": 2}} |
| 195 |
``` |
| 196 |
|
| 197 |
MongoDB doc: |
| 198 |
<https://docs.mongodb.com/manual/reference/operator/update/mul/> |
| 199 |
""" |
| 200 |
|
| 201 |
operator = "$mul" |
| 202 |
|
| 203 |
|
| 204 |
class Rename(BaseUpdateGeneralOperator): |
| 205 |
""" |
| 206 |
`$rename` update query operator |
| 207 |
|
| 208 |
MongoDB doc: |
| 209 |
<https://docs.mongodb.com/manual/reference/operator/update/rename/> |
| 210 |
""" |
| 211 |
|
| 212 |
operator = "$rename" |
| 213 |
|
| 214 |
|
| 215 |
class SetOnInsert(BaseUpdateGeneralOperator): |
| 216 |
""" |
| 217 |
`$setOnInsert` update query operator |
| 218 |
|
| 219 |
MongoDB doc: |
| 220 |
<https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/> |
| 221 |
""" |
| 222 |
|
| 223 |
operator = "$setOnInsert" |
| 224 |
|
| 225 |
|
| 226 |
class Unset(BaseUpdateGeneralOperator): |
| 227 |
""" |
| 228 |
`$unset` update query operator |
| 229 |
|
| 230 |
MongoDB doc: |
| 231 |
<https://docs.mongodb.com/manual/reference/operator/update/unset/> |
| 232 |
""" |
| 233 |
|
| 234 |
operator = "$unset" |