2014-11-24 85 views
0

我有像這樣PHP的MongoDB更新子元素

"bar" : "547244fe10f0edd3128b4567", 
    "items" : [ 
     { 
      "1" : { 
       "message" : "", 
       "display" : "true", 
       "type" : "text" 
      } 
     }, 
     { 
      "2" : { 
       "id" : "234234", 
       "type" : "image", 
       "message" : "foo", 
       "display" : "true", 
       "created_at" : NumberLong(1416432114) 
      } 
     }, 
     { 
      "3" : { 
       "message" : "", 
       "display" : "true", 
       "type" : "text" 
      } 
     }, 

項目的子元素的文檔,我試圖更新兒童的一個值

$foo['items']['1']['message'] = 'hello'; 
$story = InfoDB::where('_id', $id)->update($foo); 

這樣

 "1" : { 
      "message" : "", 
      "display" : "true", 
      "type" : "text" 
     } 

變成

 "1" : { 
      "message" : "hello", 
      "display" : "true", 
      "type" : "text" 
     } 

但是,當我運行更新命令時,它刪除文檔中的所有子項。

我必須更新整個文檔嗎?還是有另一個功能?

我使用https://github.com/jenssegers/laravel-mongodb

回答

1

既然你「項目」的每一個是它自己的一個項目都有一個,因此自己的數組的索引,你必須做出一點調整,你的代碼。所以,只是嘗試與這一個

$foo['items'][0]['1']['message'] = 'hello'; 

這種替換此代碼

$foo['items']['1']['message'] = 'hello'; 

將調用的第一項(0),然後在項目中的特定鍵(「1」)。