2017-05-21 57 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插入整体嵌入的文档。