我正在练习使用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
等
1条答案
按热度按时间n6lpvg4x1#
同样的概念使你成为
{"user": "Joy" , "total_money" : 93, "ID" : 999}
,“Total_Money”通过减去它们而变得更小,当然你不能直接这样做,就像Python(4-1左右)……
以下是代码:
我想我是用3个集合的数据来计算的
输出如下所示