我尝试过运行此查询:
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' => array("parent" => 'data'),
array("parents" => 'data')
),
array("upsert" => true)
);
但它只会更新第二个“set”参数(即array(“parents”=>'data'))。当在两个单独的查询中完成时,这些工作正常,但它一起没有 - 什么给出了什么?!
答案 0 :(得分:1)
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' => array("parent" => 'data'),
),
array("upsert" => true)
);
请记住MongoDB只接受key->值对格式的数组,即array("parents" => 'data')
应为$something => array("parents" => 'data')
或在php.ini文件中进行更改,以便允许将null值作为键。
答案 1 :(得分:0)
尝试使用多个选项
$collection->update(
array('_id' => 'mongoIDhere'),
array('$set' => array("parent" => 'data')),
array("upsert" => true, "multiple" => true)
);
“多个”选项
所有符合$条件的文件都会更新。 MongoCollection :: update()具有与MongoCollection :: remove()完全相反的行为:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是单个文档,因为数据库可能会在将来某个时候更改其默认行为。
答案 2 :(得分:0)
Try something like this.
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' =>
array(
array("parent" => 'data'),
array("parents" => 'data')
)
),
array("upsert" => true)
);
Hope this will work..
答案 3 :(得分:0)
假设您正在使用https://github.com/mongodb/mongo-php-library,则应尝试以下操作:
$collection->update(
['_id' => 'mongoIDhere],
['$set' => ['parent' => 'data', 'parent2' => 'data2']],
['upsert' => true]
);
我希望您提出问题后能找到解决问题的方法。但是,我今天也遇到了同样的问题,无法在搜索引擎中找到任何答案,所以我尽管这样做可能会帮助其他家伙。