13
13
# limitations under the License.
14
14
15
15
16
- from typing import Union , List
16
+ from typing import Union , List , Dict
17
17
18
18
import pymongo
19
19
from bson .son import SON
20
20
21
21
from .commands import AggregateCommand , FindCommand , CountCommand , \
22
- UpdateCommand , DistinctCommand , DeleteCommand
23
-
22
+ UpdateCommand , DistinctCommand , DeleteCommand , FindAndModifyCommand
24
23
25
24
Document = Union [dict , SON ]
26
25
@@ -29,10 +28,11 @@ def __init__(self, collection):
29
28
self .collection = collection
30
29
self .last_cmd_payload = None
31
30
32
- def _explain_command (self , command : Document ):
33
- explain_command = SON ([("explain" , command .get_SON ())])
31
+ def _explain_command (self , command ):
32
+ command_son = command .get_SON ()
33
+ explain_command = SON ([("explain" , command_son )])
34
34
explain_command ["verbosity" ] = "queryPlanner"
35
- self .last_cmd_payload = explain_command
35
+ self .last_cmd_payload = command_son
36
36
return self .collection .database .command (explain_command )
37
37
38
38
def update_one (self , filter , update , upsert = False ,
@@ -41,6 +41,9 @@ def update_one(self, filter, update, upsert=False,
41
41
session = None , ** kwargs ):
42
42
kwargs .update (locals ())
43
43
del kwargs ["self" ], kwargs ["kwargs" ], kwargs ["filter" ], kwargs ["update" ]
44
+ kwargs ["multi" ] = False
45
+ if bypass_document_validation == False :
46
+ del kwargs ["bypass_document_validation" ]
44
47
command = UpdateCommand (self .collection , filter , update , kwargs )
45
48
return self ._explain_command (command )
46
49
@@ -49,6 +52,8 @@ def update_many(self, filter: Document, update: Document, upsert=False,
49
52
kwargs .update (locals ())
50
53
del kwargs ["self" ], kwargs ["kwargs" ], kwargs ["filter" ], kwargs ["update" ]
51
54
kwargs ["multi" ] = True
55
+ if bypass_document_validation == False :
56
+ del kwargs ["bypass_document_validation" ]
52
57
command = UpdateCommand (self .collection , filter , update , kwargs )
53
58
return self ._explain_command (command )
54
59
@@ -61,8 +66,19 @@ def aggregate(self, pipeline: List[Document], session=None, **kwargs):
61
66
{},kwargs )
62
67
return self ._explain_command (command )
63
68
64
- def count_documents (self , filter : Document , session = None , ** kwargs ):
65
- command = CountCommand (self .collection , filter ,kwargs )
69
+ def estimated_document_count (self ,
70
+ ** kwargs ):
71
+
72
+ command = CountCommand (self .collection , None , kwargs )
73
+ return self ._explain_command (command )
74
+
75
+ def count_documents (self , filter : Document , session = None ,
76
+ ** kwargs ):
77
+
78
+ command = AggregateCommand (self .collection , [{'$match' : filter },
79
+ {'$group' : {'n' : {'$sum' : 1 }, '_id' : 1 }}],
80
+ session , {}, kwargs ,
81
+ exclude_keys = filter .keys ())
66
82
return self ._explain_command (command )
67
83
68
84
def delete_one (self , filter : Document , collation = None , session = None ,
@@ -73,8 +89,11 @@ def delete_one(self, filter: Document, collation=None, session=None,
73
89
return self ._explain_command (command )
74
90
75
91
def delete_many (self , filter : Document , collation = None ,
76
- session = None , ** kwargs ):
92
+ session = None , ** kwargs : Dict [str , Union [int , str ,
93
+ Document ,
94
+ bool ]]):
77
95
limit = 0
96
+ kwargs ["session" ] = session
78
97
command = DeleteCommand (self .collection , filter , limit , collation ,
79
98
kwargs )
80
99
return self ._explain_command (command )
@@ -100,20 +119,79 @@ def watch(self, pipeline: Document = None, full_document: Document = None,
100
119
max_await_time_ms })
101
120
return self ._explain_command (command )
102
121
103
- def find (self , filter : Document = None , projection : list = None ,
104
- skip : int = 0 , limit : int = 0 , no_cursor_timeout : bool = False ,
105
- sort : Document = None , allow_partial_results : bool = False ,
106
- oplog_replay : bool = False , batch_size : int = 0 ,
107
- collation : Document = None , hint : Union [Document , str ] = None ,
108
- max_time_ms : int = None , max : Document = None , min : Document =
109
- None , return_key : bool = False ,
110
- show_record_id : bool = False , comment : str = None ,
111
- session :Document = None , ** kwargs : Union [int , str , Document ,
112
- bool ]):
122
+ def find (self , filter : Document = None ,
123
+ ** kwargs : Dict [str , Union [int , str ,Document , bool ]]):
113
124
kwargs .update (locals ())
114
125
del kwargs ["self" ], kwargs ["kwargs" ]
115
126
command = FindCommand (self .collection ,
116
127
kwargs )
117
128
return self ._explain_command (command )
118
129
130
+ def find_one (self , filter : Document = None , ** kwargs : Dict [str ,
131
+ Union [int , str ,
132
+ Document , bool ]]):
133
+ kwargs .update (locals ())
134
+ del kwargs ["self" ], kwargs ["kwargs" ]
135
+ kwargs ["limit" ] = 1
136
+ command = FindCommand (self .collection , kwargs )
137
+ return self ._explain_command (command )
138
+
139
+ def find_one_and_delete (self , filter : Document , projection : list = None ,
140
+ sort : Document = None , session = None ,
141
+ ** kwargs ):
142
+ kwargs ["query" ] = filter
143
+ kwargs ["fields" ] = projection
144
+ kwargs ["sort" ] = sort
145
+ kwargs ["remove" ] = True
146
+ kwargs ["session" ] = session
147
+
148
+ command = FindAndModifyCommand (self .collection ,
149
+ kwargs )
150
+ return self ._explain_command (command )
151
+
152
+ def find_one_and_replace (self , filter : Document , replacement : Document ,
153
+ projection : list = None , sort = None ,
154
+ return_document = pymongo .ReturnDocument .BEFORE ,
155
+ session = None , ** kwargs ):
156
+ kwargs ["query" ] = filter
157
+ kwargs ["fields" ] = projection
158
+ kwargs ["sort" ] = sort
159
+ kwargs ["new" ] = False
160
+ kwargs ["update" ] = replacement
161
+ kwargs ["session" ] = session
162
+ command = FindAndModifyCommand (self .collection ,
163
+ kwargs )
164
+ return self ._explain_command (command )
165
+
166
+ def find_one_and_update (self , filter : Document , replacement : Document ,
167
+ projection : list = None , sort = None ,
168
+ return_document = pymongo .ReturnDocument .BEFORE ,
169
+ session = None , ** kwargs ):
170
+ kwargs ["query" ] = filter
171
+ kwargs ["fields" ] = projection
172
+ kwargs ["sort" ] = sort
173
+ kwargs ["upsert" ] = False
174
+ kwargs ["update" ] = replacement
175
+ kwargs ["session" ] = session
176
+
177
+ command = FindAndModifyCommand (self .collection ,
178
+ kwargs )
179
+ return self ._explain_command (command )
180
+
181
+ def replace_one (self , filter : Document , replacement : Document ,
182
+ upsert = False , bypass_document_validation = False ,
183
+ collation = None , session = None , ** kwargs ):
184
+ kwargs .update (locals ())
185
+ del kwargs ["self" ], kwargs ["kwargs" ], kwargs ["filter" ], kwargs [
186
+ "replacement" ]
187
+ kwargs ["multi" ] = False
188
+ if not bypass_document_validation :
189
+ del kwargs ["bypass_document_validation" ]
190
+ update = replacement
191
+ command = UpdateCommand (self .collection , filter , update , kwargs )
192
+
193
+ return self ._explain_command (command )
194
+
195
+
196
+
119
197
0 commit comments