(Pymongo)连接/添加与MongoDB中来自不同集合的两列的关系

gzjq41n4  于 2022-10-22  发布在  Go
关注(0)|答案(1)|浏览(172)

我正在练习使用python提交回滚MongoDB(Pymongo)
例如,我有两个集合,一个集合存储每个用户的所有金额,日志集合将数据与滚动或输入的钱一起存储在银行

  • total_money_collection{“用户”:“joy”,“TOTAL_Money”:100,“ID”:999}
  • log_money_collection{“User”:“joy”,“In_Out_Put_Money”:空,“ID”:999}

我需要一个简单的帮手,也许是there is a short pymongo command,或者MongoDB can do such operation that just extract the result is fine等等。
如果我从log_money_collection输入{“User”:“joy”,“In_out_Put_Money”:-7,“ID”:999},
"in_out_put_money"列如何影响"total_money"
预期产出:
total_money_collection{“用户”:“joy”,“TOTAL_Money”:93,“ID”:999}
这是我的代码(我做了很多不必要的动作,我相信有一个更简单的方法):

import pymongo
import datetime
import json
from bson.objectid import ObjectId
from bson import json_util
import re

def init_db(ip, db, coll):
    myclient = pymongo.MongoClient('mongodb://' + ip + '/')
    mydb = myclient[db]
    mycol = mydb[coll]

    return mydb, mycol, myclient

def Quy_data(  mycol , find_values_json , one_or_many_bool):
    try:   
            if one_or_many_bool:
                x = []

                for y in  mycol.find(find_values_json):
                    x.append(y)

                cash_all_value = mycol.find({},{ "Cash_$": 1 })

            else:
                x = mycol.find_one(find_values_json)
                cash_all_value = mycol.find({},{ "Cash_$": 1 })
            return x , cash_all_value
    except Exception as e:
            msg_fail_reason = "error in ins_data function"
            return msg_fail_reason

ip_input = input("Enter the ip: ")
exist_DB_name = input("Enter exist DB name: ")

def parse_json(data):
    return json.loads(json_util.dumps(data))

try:
    exist_coll = input("Enter exist collection (ex: 10_20_cash_all , 10_20_cash_log ): ")
    mydb, mycol , myclient = init_db(ip_input, exist_DB_name, exist_coll)

    with myclient.start_session(causal_consistency=True) as session:

        #  Open a transaction session 
        with session.start_transaction():

            # mycol.insert_one({'Name': ' Gosum '}, session=session)

            if exist_coll == "10_20_cash_all":
                    # I set find all ( = find )
                    one_or_many_bool = True
                    findvalues_str = input("Enter find data conditions: ")

                    find_values_json =json.loads(findvalues_str)
                    x , cash_all_value = Quy_data( mycol , find_values_json , one_or_many_bool )

                             # if someone want data in json 
                    modified_data_x_json = parse_json(x)
                    cash_all_value_json = parse_json(cash_all_value)
                    a = str(cash_all_value_json)

                    print(modified_data_x_json)
                    print(type(modified_data_x_json))

                    print("= = = = = ")
                    print(a)
                    print(type(a))
                    b = re.search("'Cash_$':  (.*)", a)
                    print(b)

except Exception as e:
    #  Output exception content 
    print(e)

My的输出(我找到用户joy,尝试提取"total_money"之后的数字,然后减去"in_out_put_money")

Enter the ip: localhost
Enter exist DB name: (practice_10_14)-0004444
Enter exist collection (ex: 10_20_cash_all , 10_20_cash_log ): 10_20_cash_all
Enter find data conditions: { "name": "Joy" }
[{'_id': {'$oid': '6348d73be94317989175dc2d'}, 'name': 'Joy', 'ID': 999, 'Age': 23, 'time': {'$date': '2022-10-17T09:11:54Z'}, 'total_money': 100}]
<class 'list'>
= = = = =
[{'_id': {'$oid': '6348d73be94317989175dc2d'}, 'total_money': 100}, {'_id': {'$oid': '6348d73be94317989175dc2e'}, 'total_money': 100}, {'_id': {'$oid': '6348d73be94317989175dc2f'}, 'total_money': 100}, {'_id': {'$oid': '6348d73be94317989175dc30'}, 'total_money': 100}, {'_id': {'$oid': '6348d73be94317989175dc31'}, 'total_money': 100}, {'_id': {'$oid': '635112dea1fa85dd0cfe590b'}, 'total_money': 100}]
<class 'str'>
None

提交回滚MongoDB账号钱的简单步骤我转了一个大圈拿到它,我需要一个简单的方法,可能是there is a short pymongo command,或者MongoDB can do so that just extract the result is fine

n6lpvg4x

n6lpvg4x1#

同样的概念使你成为{"user": "Joy" , "total_money" : 93, "ID" : 999}
“Total_Money”通过减去它们而变得更小,当然你不能直接这样做,就像Python(4-1左右)……
以下是代码:

import pymongo
import datetime
import json
from bson.objectid import ObjectId
from bson import json_util
import re

myclient = pymongo.MongoClient("mongodb://localhost/")
mydb = myclient["(practice_10_14)-0004444"]

# UD_db_data means update

def UD_db_data(mycol , myquery_json, newvalues_json, one_or_many_bool):

    if one_or_many_bool == True:
        x = mycol.update_many(myquery_json, newvalues_json)
    else:
        x = mycol.update_one(myquery_json, newvalues_json)
    return x

# Quy_data = query find db_data

def Quy_data( mycol ,find_values_json , one_or_many_bool):
    try:   
            if one_or_many_bool:
                x = []

                for y in  mycol.find(find_values_json):
                    x.append(y)

            else:
                x = mycol.find_one(find_values_json)
            return x 
    except Exception as e:
            msg_fail_reason = "error in ins_data function"
            return msg_fail_reason

mycol_one = mydb["10_20_cash_all"]
mycol_two = mydb["10_20_cash_log"]
mycol_3rd = mydb["10_20_cash_info"]

 # already store 100$ in bank

# doc_two = {"ID" :  100998 , "Cash_log$" : 5 }      # withdraw 5$ from bank

doc_two = input("Enter ID and log amount$: ")
doc_3rd = input("Enter extra info: ")

doc_two_dic = json.loads(doc_two)
doc_3rd_dic = json.loads(doc_3rd)

# doc_3rd = {"note" :  "today is good" }

ID_input = doc_two_dic['ID']
print("ur id is :" + str(ID_input))
doc_one = {"ID" :  ID_input}

with myclient.start_session() as s:
    cash_all_result = mycol_one.find_one(doc_one, session=s)

    def cb(s):
            try:
                    while True:
                            cash_all_result = mycol_one.find_one(doc_one, session=s)
                            mycol_two.insert_one(doc_two_dic, session=s)

                            # print( 3/0 )

                            mycol_3rd.insert_one(doc_3rd_dic, session=s)

                            print( "now total is :" + str(cash_all_result['Cash_$']) )
                            Cash_total_int = int(cash_all_result['Cash_$'])
                            log_int = int(doc_two_dic['Cash_log$'])

                            if Cash_total_int < log_int:
                                print("error: withdraw is over ur balance")
                                break

                            new_Cash_total = Cash_total_int - log_int
                            print("now total is :" + str(new_Cash_total))

                            newvalues_json = { "$set" : {"Cash_$" : new_Cash_total } }

                            mycol_one.update_one(doc_one , newvalues_json, session=s)

                            fail_condition_json = {"ok" : 1 , "fail reason" : "no error "}
                            print(fail_condition_json)
                            return fail_condition_json

            except Exception as e:
                    fail_condition_json = {"ok" : 0 , "fail reason" : "error raise on start_session()"}
                    print(fail_condition_json)
                    return fail_condition_json

    s.with_transaction(cb)

我想我是用3个集合的数据来计算的
输出如下所示

Enter ID and log amount$: {"ID" :  100998 , "Cash_log$" : 23 }
Enter extra info: {"note" :  "today is no good" }
ur id is :100998
now total is :72
now total is :49
{'ok': 1, 'fail reason': 'no error '}

相关问题