2011-10-11 65 views
1

我试图运行此查询:PHP MongoDB的更新与一个查询多个字段

$collection->update(
    array('_id' => 'mongoIDhere'), 
    array(
     '$set' => array("parent" => 'data'), 
     array("parents" => 'data') 
    ), 
    array("upsert" => true) 
); 

但它只会更新第二“设置”参数(这是阵列(“父母” =>“数据” ))。当在两个单独的查询完成时,这些工作正常,但它们并没有 - 什么给了?!

回答

1
$collection->update(
    array('_id' => 'mongoIDhere'), 
    array(
     '$set' => array("parent" => 'data'), 
    ), 
    array("upsert" => true) 
); 

记住的MongoDB只接受键 - >值对即格式阵列array("parents" => 'data')应该$something => array("parents" => 'data')或制作在php.ini文件中改变,因此将允许空值的关键。

0

尝试用多个选项

$collection->update(
    array('_id' => 'mongoIDhere'), 
    array('$set' => array("parent" => 'data')), 
    array("upsert" => true, "multiple" => true) 
); 

“多” 选项

所有匹配$标准文件将被更新。 MongoCollection :: update()具有与MongoCollection :: remove()完全相反的行为:它默认更新一个文档,而不是所有匹配的文档。建议您始终指定是要更新多个文档还是单个文档,因为数据库可能会在将来的某个时间点更改其默认行为。

Mongocollection in PHP Doc's

0

尝试这样的事情。

$collection->update(
      array('_id' => 'mongoIDhere'), 
      array(
       '$set' => 
          array(
           array("parent" => 'data'), 
           array("parents" => 'data') 
           ) 
      ), 
      array("upsert" => true) 
     ); 

希望这将工作..

相关问题