PHP MongoDB使用一个查询更新多个字段
我已尝试运行此查询:PHP MongoDB使用一个查询更新多个字段,php,mongodb,mongodb-php,Php,Mongodb,Mongodb Php,我已尝试运行此查询: $collection->update( array('_id' => 'mongoIDhere'), array( '$set' => array("parent" => 'data'), array("parents" => 'data') ), array("upsert" => true) ); 但它只会更新第二个“set”参数(即数组(“parents”=>“da
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' => array("parent" => 'data'),
array("parents" => 'data')
),
array("upsert" => true)
);
但它只会更新第二个“set”参数(即数组(“parents”=>“data”)。当在两个单独的查询中完成时,这些都可以很好地工作,但如果加在一起就不行了-给出了什么
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' => array("parent" => 'data'),
),
array("upsert" => true)
);
记住MongoDB只接受key->value对格式的数组,即数组(“parents”=>“data”)
应该是$something=>数组(“parents”=>“data”)
或者在php.ini文件中进行更改,以便允许将空值作为键
记住,MongoDB只接受key->value对格式的数组,即
数组(“parents”=>“data”)
应该是$something=>数组(“parents”=>“data”)
或在php.ini文件中进行更改,以便允许将空值作为键。尝试使用多个选项
$collection->update(
array('_id' => 'mongoIDhere'),
array('$set' => array("parent" => 'data')),
array("upsert" => true, "multiple" => true)
);
“多个”选项
所有符合$criteria的文档都将更新。MongoCollection::update()的行为与MongoCollection::remove()完全相反:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是要更新单个文档,因为数据库可能会在将来某个时候更改其默认行为
尝试使用多个选项
$collection->update(
array('_id' => 'mongoIDhere'),
array('$set' => array("parent" => 'data')),
array("upsert" => true, "multiple" => true)
);
“多个”选项
所有符合$criteria的文档都将更新。MongoCollection::update()的行为与MongoCollection::remove()完全相反:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是要更新单个文档,因为数据库可能会在将来某个时候更改其默认行为
试试这样的方法
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' =>
array(
array("parent" => 'data'),
array("parents" => 'data')
)
),
array("upsert" => true)
);
希望这能奏效。试试这样的方法
$collection->update(
array('_id' => 'mongoIDhere'),
array(
'$set' =>
array(
array("parent" => 'data'),
array("parents" => 'data')
)
),
array("upsert" => true)
);
希望这能起作用。假设您正在使用,您应该尝试以下方法:
$collection->update(
['_id' => 'mongoIDhere],
['$set' => ['parent' => 'data', 'parent2' => 'data2']],
['upsert' => true]
);
我希望你能找到解决问题的方法,因为你提出了这个问题。然而,我今天遇到了同样的问题,在搜索引擎中找不到任何答案,所以我认为这可能会帮助其他人。假设您正在使用,您应该尝试以下方法:
$collection->update(
['_id' => 'mongoIDhere],
['$set' => ['parent' => 'data', 'parent2' => 'data2']],
['upsert' => true]
);
我希望你能找到解决问题的方法,因为你提出了这个问题。然而,我今天面临着同样的问题,在搜索引擎上找不到任何答案,所以我认为这可能会帮助其他人