2013-06-01 2 views
0
내가 MongoDB를 저장 모델을 따랐다

의 문서를 업데이트 할 적절한 기준을 찾을 수 없습니다 :는 MongoDB를

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

나는 일명 새 문자열을 추가 2 요소 (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로 2 노드를 찾을 때 :

$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

좋아요! 그것은 작동합니다, 제발, 내가 투표 할 수 있도록 대답에 붙여 넣으십시오. –

답변