| 1 |
from beanie.odm.operators.update import BaseUpdateOperator |
| 2 |
|
| 3 |
|
| 4 |
class BaseUpdateArrayOperator(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 AddToSet(BaseUpdateArrayOperator): |
| 16 |
""" |
| 17 |
`$addToSet` update array query operator |
| 18 |
|
| 19 |
Example: |
| 20 |
|
| 21 |
```python |
| 22 |
class Sample(Document): |
| 23 |
results: List[int] |
| 24 |
|
| 25 |
AddToSet({Sample.results: 2}) |
| 26 |
``` |
| 27 |
|
| 28 |
Will return query object like |
| 29 |
|
| 30 |
```python |
| 31 |
{"$addToSet": {"results": 2}} |
| 32 |
``` |
| 33 |
|
| 34 |
MongoDB docs: |
| 35 |
<https://docs.mongodb.com/manual/reference/operator/update/addToSet/> |
| 36 |
""" |
| 37 |
|
| 38 |
operator = "$addToSet" |
| 39 |
|
| 40 |
|
| 41 |
class Pop(BaseUpdateArrayOperator): |
| 42 |
""" |
| 43 |
`$pop` update array query operator |
| 44 |
|
| 45 |
Example: |
| 46 |
|
| 47 |
```python |
| 48 |
class Sample(Document): |
| 49 |
results: List[int] |
| 50 |
|
| 51 |
Pop({Sample.results: 2}) |
| 52 |
``` |
| 53 |
|
| 54 |
Will return query object like |
| 55 |
|
| 56 |
```python |
| 57 |
{"$pop": {"results": -1}} |
| 58 |
``` |
| 59 |
|
| 60 |
MongoDB docs: |
| 61 |
<https://docs.mongodb.com/manual/reference/operator/update/pop/> |
| 62 |
""" |
| 63 |
|
| 64 |
operator = "$pop" |
| 65 |
|
| 66 |
|
| 67 |
class Pull(BaseUpdateArrayOperator): |
| 68 |
""" |
| 69 |
`$pull` update array query operator |
| 70 |
|
| 71 |
Example: |
| 72 |
|
| 73 |
```python |
| 74 |
class Sample(Document): |
| 75 |
results: List[int] |
| 76 |
|
| 77 |
Pull(In(Sample.result: [1,2,3,4,5]) |
| 78 |
``` |
| 79 |
|
| 80 |
Will return query object like |
| 81 |
|
| 82 |
```python |
| 83 |
{"$pull": { "results": { $in: [1,2,3,4,5] }}} |
| 84 |
``` |
| 85 |
|
| 86 |
MongoDB docs: |
| 87 |
<https://docs.mongodb.com/manual/reference/operator/update/pull/> |
| 88 |
""" |
| 89 |
|
| 90 |
operator = "$pull" |
| 91 |
|
| 92 |
|
| 93 |
class Push(BaseUpdateArrayOperator): |
| 94 |
""" |
| 95 |
`$push` update array query operator |
| 96 |
|
| 97 |
Example: |
| 98 |
|
| 99 |
```python |
| 100 |
class Sample(Document): |
| 101 |
results: List[int] |
| 102 |
|
| 103 |
Push({Sample.results: 1}) |
| 104 |
``` |
| 105 |
|
| 106 |
Will return query object like |
| 107 |
|
| 108 |
```python |
| 109 |
{"$push": { "results": 1}} |
| 110 |
``` |
| 111 |
|
| 112 |
MongoDB docs: |
| 113 |
<https://docs.mongodb.com/manual/reference/operator/update/push/> |
| 114 |
""" |
| 115 |
|
| 116 |
operator = "$push" |
| 117 |
|
| 118 |
|
| 119 |
class PullAll(BaseUpdateArrayOperator): |
| 120 |
""" |
| 121 |
`$pullAll` update array query operator |
| 122 |
|
| 123 |
Example: |
| 124 |
|
| 125 |
```python |
| 126 |
class Sample(Document): |
| 127 |
results: List[int] |
| 128 |
|
| 129 |
PullAll({ Sample.results: [ 0, 5 ] }) |
| 130 |
``` |
| 131 |
|
| 132 |
Will return query object like |
| 133 |
|
| 134 |
```python |
| 135 |
{"$pullAll": { "results": [ 0, 5 ] }} |
| 136 |
``` |
| 137 |
|
| 138 |
MongoDB docs: |
| 139 |
<https://docs.mongodb.com/manual/reference/operator/update/pullAll/> |
| 140 |
""" |
| 141 |
|
| 142 |
operator = "$pullAll" |