2017-11-23 1 views
1

I는 다음과 같습니다 모델이 있습니다

contacts: { 
    phones: [{ 
    phone_id: { 
     type: ObjectId 
    }, 
    phone_number: { 
     type: String 
    } 
    }], 
    shared: { 
    phones: [{ 
     phone_id: { 
     type: ObjectId 
     }, 
     phone_number: { 
     type: String 
     }, 
    }] 
    } 
} 

내가 내 쿼리에서 통과 ID를 일치하는 전화 번호를 삭제하려고 경우, 엔드 포인트가를 끈.

내 코드는 다음과 같습니다

Contacts.update({ 'phones._id': req.params.phone_id }, 
    { "$pull": { "phones": { "_id": req.params.phone_id} }}, 
    { safe: true, multi:true }, function(err, obj) { 
    }); 

    Contacts.update({ 'shared.phones._id': req.params.phone_id }, 
    { "$pull": { "shared.phones": { "_id": req.params.phone_id} }}, 
    { safe: true, multi:true }, function(err, obj) { 
    }); 

배열이 작동하는 전화기 형태의 수를 제거하는 첫 번째 업데이트.

하지만 'shared.phones'배열에서 번호를 삭제해야하는 두 번째 버전은 그렇지 않습니다.

실종에 대한 아이디어가 있습니까?

나는이 같은 '$'위치 연산자를 사용하여 시도 :

Contacts.update({ 'shared.phones.$._id': req.params.phone_id }, 
    { "$pull": { "shared.phones.$": { "_id": req.params.phone_id} }}, 
    { safe: true, multi:true }, function(err, obj) { 
    }); 

을하지만 그것은 잘 작동하지 않았다.

미리 감사드립니다.

+0

시도해보십시오. {$ pull : { 'shared.phones._id': req.params.phone_id}}' –

+1

고마워, 그레고리! 그거였다. – cnak2

답변

1

로는 MongoDB의 깊은 하나 개 이상의 수준에 가고 싶어하지 않기 때문에 항상 하나의 키에 모두 사용하는 의견

{ $pull: { 'shared.phones._id': req.params.phone_id } } 

이 중첩 된 배열에 관해서

말했다.