2016-08-25 2 views
1

내 데이터는 다음과 같습니다. 이제 favoriteapi는 키/값 레코드가 들어오고 나옵니다.Mongodb 중첩 삽입

어떻게 내가이

var updatedocument =  
    { 
     "api_id" : "sanket", 
     "api_name" : "sanket" 
    } 

user.favoriteapi.insert(updatedocument, options); 

하지만, 행운을 시도이 중첩 된 요소

{ 

"firstname": "ram5656", 
"lastname": "rahul", 
"email": "[email protected]", 
"password": "encrypt", 
"favoriteapi" : [ 
    { 
     "api_id" : "function", 
     "api_name" : "enterprise" 
    }, 
    { 
     "api_id" : "data_subject_category", 
     "api_name" : "address" 
    }, 
    { 
     "api_id" : "data_subject", 
     "api_name" : "address" 
    } 
], 
"is_active": true, 
"create_ts": "2016-05-18T00:00:00.000Z", 
"create_by": "[email protected]", 
"update_ts": "2016-05-18T06:00:00.000Z", 
"update_by": "[email protected]", 
"lastlogin": "2016-05-01T00:00:00.000Z" 

}

에 데이터를 삽입 할 수 있습니다.

업서 트를 사용해야합니까?

답변

0

당신은 사용자 문서에 update을 할 필요가, 당신은 $pushfavouriteapi 배열에 새 하위 문서를 지정할.

var updatedocument = { 
    "api_id" : "sanket", 
    "api_name" : "sanket" 
}; 

db.user.update(
    {"email": "[email protected]"}, 
    {$push: { 
     "favouriteapi" : updatedocument 
    } 
    } 
); 
+0

감사합니다. Vince :) –