2017-05-21 58 views
0

我嘗試在文檔的MongoDB更新嵌套參數:更新MongoDB中的嵌套參數?

$this->collection->update(
    ["prototype_id" => $id], 
    ["$set" => ["parameters" => $newdata]], 
    ["upsert" => true, "multiple" => true]); 

在結果我得到錯誤:

zero-length keys are not allowed, did you use $ with double quotes?

其中​​是:

array:1 [ 
    "5920d086470e6cb30e3c986c" => array:1 [ 
    "Acceleration" => "2" 
    ] 
] 
+0

什麼是您的$ id值和$設置值 –

+0

Php試圖插入它認爲變量爲'「$ set」'。使用單引號傳遞字符串文字''$ set'' –

+0

它可以工作,但是這個查詢在文檔中再次插入新的參數,而不是更新 – Blablacar

回答

0

使用dot notation添加/覆蓋領域在嵌入式文件中。

喜歡的東西

$parameter_id = "5920d086470e6cb30e3c986c";

['$set' => ["parameters.".$parameter_id => ["Acceleration" => "2"]]];

可替換地,限定parameters如嵌入式陣列並且可以$push插入整體嵌入的文檔。