1212from urllib import parse
1313
1414import pymongo
15- from pymongo import MongoClient , UpdateOne
15+ from pymongo import MongoClient , UpdateOne , UpdateMany
1616from pymongo .collection import Collection
1717from pymongo .database import Database
1818from pymongo .errors import DuplicateKeyError , BulkWriteError
2323
2424class MongoDB :
2525 def __init__ (
26- self ,
27- ip = None ,
28- port = None ,
29- db = None ,
30- user_name = None ,
31- user_pass = None ,
32- url = None ,
33- ** kwargs ,
26+ self ,
27+ ip = None ,
28+ port = None ,
29+ db = None ,
30+ user_name = None ,
31+ user_pass = None ,
32+ url = None ,
33+ ** kwargs ,
3434 ):
3535 if not ip :
3636 ip = setting .MONGO_IP
@@ -97,7 +97,7 @@ def get_collection(self, coll_name, **kwargs) -> Collection:
9797 return self .db .get_collection (coll_name , ** kwargs )
9898
9999 def find (
100- self , coll_name : str , condition : Optional [Dict ] = None , limit : int = 0 , ** kwargs
100+ self , coll_name : str , condition : Optional [Dict ] = None , limit : int = 0 , ** kwargs
101101 ) -> List [Dict ]:
102102 """
103103 @summary:
@@ -136,13 +136,13 @@ def find(
136136 return dataset
137137
138138 def add (
139- self ,
140- coll_name ,
141- data : Dict ,
142- replace = False ,
143- update_columns = (),
144- update_columns_value = (),
145- insert_ignore = False ,
139+ self ,
140+ coll_name ,
141+ data : Dict ,
142+ replace = False ,
143+ update_columns = (),
144+ update_columns_value = (),
145+ insert_ignore = False ,
146146 ):
147147 """
148148 添加单条数据
@@ -198,13 +198,13 @@ def add(
198198 return affect_count
199199
200200 def add_batch (
201- self ,
202- coll_name : str ,
203- datas : List [Dict ],
204- replace = False ,
205- update_columns = (),
206- update_columns_value = (),
207- condition_fields : dict = None ,
201+ self ,
202+ coll_name : str ,
203+ datas : List [Dict ],
204+ replace = False ,
205+ update_columns = (),
206+ update_columns_value = (),
207+ condition_fields : dict = None ,
208208 ):
209209 """
210210 批量添加数据
@@ -362,11 +362,11 @@ def update_many(self, coll_name, data: Dict, condition: Dict, upsert: bool = Fal
362362 return True
363363
364364 def update_batch (
365- self ,
366- coll_name : str ,
367- update_data_list : List [Dict ],
368- condition_field : str ,
369- upsert : bool = False ,
365+ self ,
366+ coll_name : str ,
367+ update_data_list : List [Dict ],
368+ condition_field : str ,
369+ upsert : bool = False ,
370370 ):
371371 """
372372 批量更新数据
@@ -387,7 +387,7 @@ def update_batch(
387387
388388 for update_data in update_data_list :
389389 condition = {condition_field : update_data .get (condition_field )}
390- update_operation = UpdateOne (
390+ update_operation = UpdateMany (
391391 condition , {"$set" : update_data }, upsert = upsert
392392 )
393393 bulk_operations .append (update_operation )
@@ -468,7 +468,7 @@ def get_index_key(self, coll_name, index_name):
468468 return index_keys
469469
470470 def __get_update_condition (
471- self , coll_name : str , data : dict , duplicate_errmsg : str
471+ self , coll_name : str , data : dict , duplicate_errmsg : str
472472 ) -> dict :
473473 """
474474 根据索引冲突的报错信息 获取更新条件
0 commit comments