是否有更传统的方法来检查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
是看是否发送设置为true。如果是我不想执行doSomething();
谢谢,这工作很好。我修复了代码,使其正确读取。这个'''!Donate.findOne({'debit.id':debitID})。credit.sent'''的原因是查看发送是否设置为true。如果是我不想执行'''doSomething();''' – JoshJoe 2014-10-16 20:38:06
啊,现在有道理。 – richsilv 2014-10-16 20:39:14