I have a data in MongoDB like this
{"_id":{"$oid":"5ec4ed0b94996124187b95ce"},
"cid":"245345323",
"dtl":[
{"_id":{"$oid":"5ed5ab655df7930ec424be1e"},
"sid":"test",
"amt":{"$numberInt":"48"},
"Name":"test"},
{"_id":{"$oid":"5ed5ac9f79a75423d4bc0270"},
"sid":"s",
"amt":{"$numberInt":"14"},
"Name":"sfds"},
{"_id":{"$oid":"5ed5ace7d204ab144863ed7a"},
"sid":"wesad",
"amt":{"$numberInt":"6"},
"Name":"2sdad"
},
"updated":{"$date":{"$numberLong":"1589964043787"}},
"__v":{"$numberInt":"6"},
"TotalAmt":{"$numberInt":"68"}}
I need to update the TotalAmt field only if amt field in the array has value greater than 10. I have used the below 2 options
PM.findOneAndUpdate(
{
_id: MongoId,
},
[
{
$set: {
//TotalAmt: { $sum: '$dtl.amt' },
/*TotalAmt: {
$cond: {
if: { $gte: ['$dtl.amt', 10] },
then: { $sum: '$dtl.amt' },
else: 0,
},
},*/
},
},
],
{ new: true }
)
It works fine without amt > 10 condition (first option commented). But not working if I add the condition. Any help would be appreciated. Thanks