Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复mongodb批量更新时,多个满足条件的数据只更新一条的问题. #263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions feapder/db/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from urllib import parse

import pymongo
from pymongo import MongoClient, UpdateOne
from pymongo import MongoClient, UpdateOne, UpdateMany
from pymongo.collection import Collection
from pymongo.database import Database
from pymongo.errors import DuplicateKeyError, BulkWriteError
Expand All @@ -23,14 +23,14 @@

class MongoDB:
def __init__(
self,
ip=None,
port=None,
db=None,
user_name=None,
user_pass=None,
url=None,
**kwargs,
self,
ip=None,
port=None,
db=None,
user_name=None,
user_pass=None,
url=None,
**kwargs,
):
if not ip:
ip = setting.MONGO_IP
Expand Down Expand Up @@ -97,7 +97,7 @@ def get_collection(self, coll_name, **kwargs) -> Collection:
return self.db.get_collection(coll_name, **kwargs)

def find(
self, coll_name: str, condition: Optional[Dict] = None, limit: int = 0, **kwargs
self, coll_name: str, condition: Optional[Dict] = None, limit: int = 0, **kwargs
) -> List[Dict]:
"""
@summary:
Expand Down Expand Up @@ -136,13 +136,13 @@ def find(
return dataset

def add(
self,
coll_name,
data: Dict,
replace=False,
update_columns=(),
update_columns_value=(),
insert_ignore=False,
self,
coll_name,
data: Dict,
replace=False,
update_columns=(),
update_columns_value=(),
insert_ignore=False,
):
"""
添加单条数据
Expand Down Expand Up @@ -198,13 +198,13 @@ def add(
return affect_count

def add_batch(
self,
coll_name: str,
datas: List[Dict],
replace=False,
update_columns=(),
update_columns_value=(),
condition_fields: dict = None,
self,
coll_name: str,
datas: List[Dict],
replace=False,
update_columns=(),
update_columns_value=(),
condition_fields: dict = None,
):
"""
批量添加数据
Expand Down Expand Up @@ -362,11 +362,11 @@ def update_many(self, coll_name, data: Dict, condition: Dict, upsert: bool = Fal
return True

def update_batch(
self,
coll_name: str,
update_data_list: List[Dict],
condition_field: str,
upsert: bool = False,
self,
coll_name: str,
update_data_list: List[Dict],
condition_field: str,
upsert: bool = False,
):
"""
批量更新数据
Expand All @@ -387,7 +387,7 @@ def update_batch(

for update_data in update_data_list:
condition = {condition_field: update_data.get(condition_field)}
update_operation = UpdateOne(
update_operation = UpdateMany(
condition, {"$set": update_data}, upsert=upsert
)
bulk_operations.append(update_operation)
Expand Down Expand Up @@ -468,7 +468,7 @@ def get_index_key(self, coll_name, index_name):
return index_keys

def __get_update_condition(
self, coll_name: str, data: dict, duplicate_errmsg: str
self, coll_name: str, data: dict, duplicate_errmsg: str
) -> dict:
"""
根据索引冲突的报错信息 获取更新条件
Expand Down