【发布时间】:2014-10-16 19:25:55
【问题描述】:
是否有更常规的方法来检查 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是看send是否设置为true。如果是我不想执行doSomething();
【问题讨论】: