我有以下文档:
{
"dataset_path":"path_of_dataset",
"project_1":{
"model_1":"path_of_model_1",
"model_2":"path_of_model_2"
}
}
我想更改"project_1">到"renamed_project_1">和"path_of_model_1">到"new_model_1_path">。结果输出应该如下所示:
{
"dataset_path":"path_of_dataset",
"renamed_project_1":{
"renamed_model_1":"new_model_1_path",
"model_2":"path_of_model_2"
}
}
这是我尝试的:
db.collection.update_many({'dataset_path': 'path_to_dataset'}, {'$rename': {"project_1": "renamed_project_1"}},
{'$set': {"project_1.model_1": "new_model_1_path"}})
,但上面的查询抛出以下错误:
pymongo.errors。WriteError:更新路径X会产生冲突在x
这是因为您试图在单个查询中两次改变project_1
字段。蒙戈只是不知道该怎么处理。您应该考虑拆分两个操作:
db.collection.update_many({'dataset_path': 'path_to_dataset'}, {'$rename': {"project_1": "renamed_project_1"}})
db.collection.update_many({'dataset_path': 'path_to_dataset'}, {'$set': {"renamed_project_1.model_1": "new_model_1_path"}})
db.collection.update_many({}, {"$rename": {"old_value": "new_value"}})