속성과 자식 속성이 MongoDB 문서에 존재하는지 확인하는보다 일반적인 방법이 있습니까?MongoDB는 속성이 존재하는지 확인합니다 (자식 속성)
지금은 속성 또는 전체 문서 중 하나가 존재하지 않을 때 오류가 발생하지 않도록이 작업을 수행하고 있습니다.
//Check to see if the document exists
if(Donate.findOne({'debit.id': debitID})) {
//Check to see if the document has the property "credit"
if(Donate.findOne({'debit.id': debitID}).credit){
//Check to see if the object credit has the property sent
if(!Donate.findOne({'debit.id': debitID}).credit.sent){
doSomething();
}
}
}
!Donate.findOne({'debit.id': debitID}).credit.sent
은 sent가 true로 설정되어 있는지 확인합니다. 실행하고 싶지 않다면 doSomething();
감사합니다. 코드가 올바르게 읽혀 지도록 코드를 수정했습니다. 이'''! Donate.findOne ({ 'debit.id': debitID}). credit.sent'''의 이유는 sent가 true로 설정되었는지 확인하는 것입니다. 그것이 "''doSomething();''을 실행하고 싶지 않다면''' – JoshJoe
아, 이제 이해가됩니다. – richsilv