2013-06-01 19 views
0

我按照存儲在MongoDB的模型更新文件:找不到合適的標準,在MongoDB中

{ 
    "_id" : "some_table_name", 
"content" : [{ 
    "id" : "1", 
    "locname" : "KKH" 
}, { 
    "id" : "2", 
    "locname" : "Singapore National Eye Centre" 
}] 
} 

我試圖找到標準來更新第二個元素(ID = 2)又名添加新的字符串。

"new_element" : "foo"

所以新的觀點應該是:

{ 
    "_id" : "some_table_name", 
"content" : [{ 
    "id" : "1", 
    "locname" : "KKH" 
}, { 
    "id" : "2", 
"locname" : "Singapore National Eye Centre" 
    "new_element" : "foo" 
}] 
} 

表PHP

當我試圖找到ID第二個節點使用:

$array = $collection_bios2->findOne(
       array("_id" => "some_table_name", "content.id" => "2"), 
       array("_id" => 0, "content.$" => 1) 
     ); 

但當我嘗試更新它時,新節點e content下nters:

$newdata = array('$set' => array("new_element" => "foo")); 
$collection_bios2->update(
         array("_id" => "some_table_name", "content.id" => "2"),      
         $newdata 
         ); 

我得到:

{ 
    "_id" : "some_table_name", 
"content" : [{ 
    "id" : "1", 
    "locname" : "KKH" 
}, { 
    "id" : "2", 
    "locname" : "Singapore National Eye Centre" 
}], 
"new_element" : "foo" 
} 

請告訴我錯在我的執行?

請,幫助,

馬克西姆

+0

您需要使用這樣的位置運算符:'array('$ set'=> array('content。$。new_element':'foo'))' – Sammaye

+0

太棒了!它的工作原理,請貼上答案,讓我投票 –

回答