2012-08-28 2 views
0

아래 문서에는 imAccounts라는 array이 있습니다. 나는 array에있는 물체가 update()이되고 싶다. 어떻게해야합니까? 이 문서 구조mongodb에서 전체 개체를 업데이트하는 방법은 무엇입니까?

{ 
"_id" : ObjectId("503c55da1c192a530b000001"), 
"imAccounts" : [ 
    { 
    "accountType"  : "Windows", 
    "accountName"  : "rwqerqw", 
    "accountPassword" : "erqwerwe" 
    }, 
    {  
    "accountType"  : "Yahoo",  
    "accountName"  : "rwqerqw", 
    "accountPassword" : "erqwerwe" 
    }] 
} 

답변

1

에는 positional operator ($)하여 배열 객체를 갱신 할 수 위치 운영자는 현재 쿼리의 유사한 아이템에 적용

db.myarray.update(
    { // Match criteria 
     "_id" : ObjectId("503c55da1c192a530b000001"), 
     'imAccounts.accountType': 'Yahoo', 
    }, 
    { // Update first matched item using positional operator $ 
     '$set' : { 'imAccounts.$.password':'sekrit'} 
    } 
) 

참고.

+0

답변 해 주셔서 감사합니다. 제대로 작동합니다. – user1629448

관련 문제