2011-10-11 67 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) 
     ); 

希望這將工作..

相關問題