我在尝试在 python 字典中添加新的键值对同时保留以前的键值对时遇到问题。我正在MongoDB用作数据库。
我的示例响应是
"field1" : "a",
"field2" : "b",
"field3" : "c",
"history" : {
"2019-09-03 00:00:00" : "state1"
}
预期的反应是
"field1" : "a",
"field2" : "b",
"field3" : "c",
"history" : {
"2019-09-01 00:00:00" : "state1"
"2019-09-02 00:00:00" : "state1"
"2019-09-03 00:00:00" : "state1"
}
我想在历史记录中添加键值对,键是日期,值是状态,但问题是我的代码删除了以前的键值对,然后添加了一个新的键值对。
我正在使用mongo client在 MongoDB 数据中保存记录。
这是我的代码
out = dict()
history = dict()
out['field1'] = 'a'
out['filed2'] = 'b'
out['field3'] = 'c'
history[str(datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0))] = 'state1'
out_handle.update_one(
{'field1': a, 'field2': 'b', 'field3': 'c'},
{'$set': out}},
upsert=True
)
富国沪深
慕后森
相关分类