我正在尝试修改单个字段值,但是每当我这样做时,它会删除所有其他字段,只留下更新的字段,如何修改单个字段而不影响其他字段?到目前为止,这就是我正在做的事情:// $updateResult2 = $coll_rutas->updateOne( /* LIMIT TO ROUTE ID */ [ "_id" => new \MongoDB\BSON\ObjectId($ruta_id) ], /* UPDATE VALUE */ [ '$set' => [ 'destinos.$[d]' => [ "horas" => "1", "minutos" => "15", "millas" => "150", "orden" => "1", "notas" => "demo notes", ], ], ], /* WHERE FILTER */ [ "upsert" => true, 'arrayFilters' => [ ['d.ciudad_id' => 47] ] ] ); //var_dump($updateResult2); exit;这是原始的 mongo 对象:"destinos": [ { "ciudad_id": "47", "nombre_destino": "Salt Lake City, Utah", "horas": "0", "minutos": "0", "millas": "0", "orden": 1, "notas": null, "origenes": [ { "ciudad_id": "45", "nombre_origen": "Provo, Utah", "orden": 1, "notas": null, "base_price": 0 } ] } ],通过我的更改,它会删除 origins 数组、city_id 和目的地名称,保持这种状态:"destinos": [ { "horas": "1", "minutos": "15", "millas": "150", "orden": "1", "notas": "demo notes" ] } ],
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
使用此代码:
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
]
您正在定位destinos.$[d]元素并用新的数据结构替换其所有内容。
您需要使用 .(dot) 表示法来定位嵌套数组中的每个元素。这有点冗长,但这会起作用:
'$set' => [
'destinos.$[d].horas' => "1",
'destinos.$[d].minutos' => "15",
'destinos.$[d].millas' => "150",
'destinos.$[d].orden' => "1",
'destinos.$[d].notas' => "demo notes"
]
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报
0/150
提交
取消