鉴于此文档结构:
{
"_id": date,
"list": [
{ hour: "something",
hour: "something else"
}
]
}
我尝试过的事情:
col.update({'_id':my_str}, {'_id':my_str, 'list':{'$addtoset':["14":"yet another thing"]}}, {'$upsert':'true'})
编辑:
更新了我的结构:
{
_id: date,
hours: [
{ "0": "something"},
{ "1": "something else"},
...
]
}
答案 0 :(得分:0)
你不能用pymongo直接修改内部列表属性,需要拉整个文档并重新发布它:
my_doc = col.find_one({'_id': my_str})
if my_doc is not None:
# Document exist, modify it
my_doc['hours'].append({"14": "yet another thing"})
col.update({'_id': my_str}, my_doc)
else:
# Insert the new document with all the new attributes
col.insert({'_id': my_str, 'hours': [{"14": "yet another thing"}]})