From ec452bf060e2debfb086c0fcc58f3cf1c19b645c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Tue, 22 Mar 2022 10:34:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E8=AE=A4=E8=AF=81=E6=88=90?= =?UTF-8?q?=E5=8A=9F=20=E5=B7=A5=E5=95=86=E4=BF=A1=E6=81=AF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=87=86=E5=A4=87=20=E5=8D=87=E7=BA=A7=E5=88=B0v0.21?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- company/db.py | 13 +++++++++++++ company/scripts.py | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/company/db.py b/company/db.py index f0273dc..bf670fa 100644 --- a/company/db.py +++ b/company/db.py @@ -3,6 +3,7 @@ import pymongo client_tfse = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('root', 'sromitdTW569kC#M', '116.63.130.34', 27018)) client_tyc = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('root', 'gP%40DwMSVd5Sh6EiH', '116.63.130.34', 27019)) client_admin = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('root', 'RYIHrqml#LSW6#!*', '116.63.130.34', 27020)) +client_tfse_021 = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format('root', 'UTlC9cCoglD1cI1*', '116.63.130.34', 27021)) def find_data_in_tyc(param1, param2, param3): @@ -82,3 +83,15 @@ def update_data(param1, param2, param3, param4): info = res.raw_result['updatedExisting'] return info + +def UPSERT_SINGLE_DATA(param1, param2, param3, param4): + """ + 插入单条数据 + Parameters: + param1: str 数据库 + param2: str 数据表 + param3: dict 查询条件 + param4: dict 更新或新插入的数据 + """ + collection = client_tfse_021[param1][param2] + collection.update_one(param3, {"$set": param4}, upsert=True) diff --git a/company/scripts.py b/company/scripts.py index 7a03eca..9a917e3 100644 --- a/company/scripts.py +++ b/company/scripts.py @@ -6,7 +6,8 @@ import requests import pandas as pd from common.scripts import read_json_file, sub_dict, df_iterrows -from company.db import find_data_in_tyc, insert_data_to_tfse, find_data_in_tfse, conserve_data_to_tfse, update_data +from company.db import find_data_in_tyc, insert_data_to_tfse, find_data_in_tfse, conserve_data_to_tfse, update_data, \ + UPSERT_SINGLE_DATA from rating.scripts.risk_detail import associate_risk_detail, change_log_detail @@ -108,7 +109,7 @@ def basic_info_etl(cid, company_name): # 保存处理后的数据 def save_result(): - insert_data_to_tfse('企业', '公司基本信息', data) + UPSERT_SINGLE_DATA('企业数据', '基本工商信息', {"企业ID": cid}, data) # 执行方法 business_data()