我有以下文件:
{"hostname": "myhost1.com", "services": { ... } }
我想做的是:
dataset = requests.get('http://endpoint.com/hardware.json').json()
for hostname, services in dataset[0].items():
db.titleHardware.update_one({'hostname':hostname},
{services.keys()[0]: services.values()[0]},
True) #upsert
但是,我得到了以下错误:
ValueError: update only works with $ operators
是否有方法基于"hostname"
键完成整个"services"
块的更新(并最终在hostname
不存在时插入新文档)?我知道我可以编写逻辑来比较MongoDB中的内容和我试图更新/插入的内容,但我希望MongoDB中可能已经存在一些内容,或者我可以使用一些内容。
使用“替换一”替换文档。
你看过mongodb文档吗? 必须指定更新运算符,如
$set
:相关问题 更多 >
编程相关推荐