我正在学习mongodb并在龙卷风中使用python。
表中存储的数据:
t5 = [
{
"Pid" : "1111",
"Rid": "666",
"Amount": 3000
}
{
"Pid" : ["1111","2222"],
"Rid": "777",
"Amount": 4000
}
]
我想更新表格中的数据值:
h = [
{"Pid" : "1111",
"Rid": "666"
"Amount": 2000
}
]
但是我收到了错误,
我想要的输出是这样的,我只想更新Amount
[{
"Pid" : "1111",
"Rid": "666"
"Amount": 2000
}
{
"Pid" : "1111",
"Rid": "777"
"Amount": 0
}]
since there is no match for the second Rid: 777 I want the amount to be 0
Amount : 4000
用于Pid:2222
,这就是为什么它应该设置为0
我尝试使用像这样的itertools
per_id = {}
for info in chain(h, t5):
print("info", info)
per_id.setdefault(info['Rid'], {}).update(info)
res = list(per_id.values())
但它不起作用,我得到的输出为:
[{
"Pid" : "1111",
"Rid": "666"
"Amount": 2000
}
{
"Pid" : "1111",
"Rid": "777"
"Amount": 4000
}]
我怎样才能在mongodb中实现这一点,请任何人帮助我