(MongoDB & Python) connect/adding relationship with two column from different collections in MongoDB

I’m practicing use python to commit rollback MongoDB (Pymongo) and relate two or more collection’s columns together

For example, I have two collections, one collection store all amount of money of each user and log collection is saving data of with roll or input money in bank

  • total_money_collection {“user”: “Joy” , “total_money” : 100, “ID” : 999}
  • log_money_collection {“user”: “Joy” , “in_out_put_money” : null , “ID” : 999}

I need a hand for simpler way, maybe there is a short pymongo command,or MongoDB can do such operation that just extract the result is fine etc.

If I input from log_money_collection {“user”: “Joy” , “in_out_put_money” : -7 , “ID” : 999},
how can "in_out_put_money" column effect "total_money" column

Expected output:

total_money_collection {“user”: “Joy” , “total_money” : 93, “ID” :

This is my code (I made lot of make an unnecessary move, I believe there is a simpler way):

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):
            if one_or_many_bool:
                x = []
                for y in  mycol.find(find_values_json):
                cash_all_value = mycol.find({},{ "Cash_$": 1 })

                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))

    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("= = = = = ")
                    b = re.search("'Cash_$':  (.*)", a)

except Exception as e:
    #  Output exception content 

output for my (I find the user Joy, and try to extract the number after "total_money" then subtract "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'>

A simple step of commit rollback MongoDB with account money I turn a huge circle to get it, I need a hand for simpler way, maybe there is a short pymongo command,or MongoDB can do so that just extract the result is fine etc