当我更新文档时,它是完美的工作方式,但是当我删除或删除文档时,它却无法工作。它正在运行一个无限循环。
工作正常:
db.loans.find({ _deleted: false, _completed: false, _disbursed: false, _defaulted: false }).forEach(function (myLoans) {
print("Loan: " + myLoans._id + "User: " + myLoans.userId);
db.loans.update({ _id: myLoans._id }, { $set: { _deleted: true } });
});
不起作用:
db.loans.find({ _deleted: false, _completed: false, _disbursed: false, _defaulted: false }).forEach(function (myLoans) {
print("Loan: " + myLoans._id + "User: " + myLoans.userId);
db.fees.deleteOne({ _loanId: myLoans._id, _paid: false });
});
贷款对象:
db.loans.find({_ id:ObjectId(“ 5e1edd2812909d3d76f49908”)})。pretty()
{
"_id" : ObjectId("5e1edd2812909d3d76f49908"),
"manual" : {
"_manual" : false,
"timestamp" : ISODate("2020-01-15T09:36:40.169Z")
},
"additional_fee" : 0,
"byApp" : true,
"emi_type" : 1,
"_approved" : true,
"_deleted" : false,
"_booked" : false,
"_disbursed" : false,
"_completed" : false,
"_defaulted" : false,
"userId" : ObjectId("5b1cd3bc1a046fd13b51682f"),
"principal" : 2000,
"total_interest" : 112,
"total_repayment" : 2112,
"lender_interest_rate" : 0.03,
"company_interest_rate" : 0.056,
"lpf_interest_rate" : 0,
"lender_interest_amount" : 60,
"company_interest_amount" : 52,
"lpf_interest_amount" : 0,
"lender_finding_fee" : 0,
"emi_count" : 1,
"emi" : [
{
"lenderSettlement" : 0,
"companySettlement" : 0,
"escrowSettlement" : 0,
"rewardOngoing" : [ ],
"rewardCompleted" : [ ],
"autoLend" : false,
"_settled" : false,
"_disbursed" : false,
"_completed" : false,
"_id" : ObjectId("5e1edd2812909d3d76f49909"),
"principal" : 2000,
"lender_amount" : 2060,
"total_repayment" : 2112,
"total_interest" : 112,
"lender_interest_amount" : 60,
"company_interest_amount" : 52,
"lpf_addition" : 0,
"lpf_deduction" : 0,
"lpf_net" : 0,
"due_date" : ISODate("2020-03-16T09:36:40.169Z")
}
],
"status" : "Approved",
"applied_timestamp" : ISODate("2020-01-15T09:36:40.171Z"),
"_loanId" : "f49908",
"loan_type" : "A",
"__v" : 0,
"approved_timestamp" : ISODate("2020-01-15T12:49:48.699Z"),
"fees" : ObjectId("5e1f0a6c760e31012d39cb2c"),
"lenderId" : ObjectId("593115365b64cc1b036faaf9")
}
费用对象:
{ "_id" : ObjectId("5e1f0a6c760e31012d39cb2c"), "_paid" : false, "userId" : ObjectId("5b1cd3bc1a046fd13b51682f"),
"amount" : 200, "description" : "Processing Fee",
"_loanId" : ObjectId("5e1edd2812909d3d76f49908"), "timestamp" : ISODate("2020-01-15T12:49:48.695Z"),
"createdAt" : ISODate("2020-01-15T12:49:48.696Z"), "updatedAt" : ISODate("2020-01-15T12:49:48.696Z"), "__v" : 0 }
答案 0 :(得分:0)
请尝试这样
db.loans.find({
$and: [
{_deleted: false},
{_completed: false},
{_disbursed: false},
{_defaulted: false}
]
}).forEach(doc => {
print("Loan: " + doc._id + "User: " + doc.userId);
db.fees.deleteMany({
_loanId: doc._id,
_paid: false
});
});