I am trying to update single object inside an array that exist inside another array (nested array) but what happen that all records are removed and the new records are inserted instead this is the structure I am using:
"summary_mark" : [
{
"target_id" : "5aa27e77967c552b10ea725b",
"primitives" : [
{
"primitive" : "B",
"test_count" : NumberInt(3),
"right_test_count" : NumberInt(3),
"mark" : NumberInt(66)
},
{
"primitive" : "T",
"test_count" : NumberInt(3),
"right_test_count" : NumberInt(3),
"mark" : NumberInt(66)
},
{
"primitive" : "H",
"test_count" : NumberInt(3),
"right_test_count" : NumberInt(3),
"mark" : NumberInt(66)
}
]
}
],
what i am trying to do is changing one of the records inside the primitives array.
actually i am trying to update the record using this function:
$testCount = ++$primitiveSummaryMark['test_count'];
$update['summary_mark.$.primitives'][$primKey]['primitive'] = $answer->primitive_value;
$update['summary_mark.$.primitives'][$primKey]['test_count'] = $testCount;
if ($answer->result != 0) {
$rightAnswersCount = ++$primitiveSummaryMark['right_test_count'];
$update['summary_mark.$.primitives'][$primKey]['right_test_count'] = $rightAnswersCount;
} else {
$rightAnswersCount = $primitiveSummaryMark['right_test_count'];
}
if ($testCount < $level_tests_amount->tests_count[$target_level]) {
$totalTestCount = $level_tests_amount->tests_count[$target_level];
} else {
$totalTestCount = $testCount;
}
$primitiveTargetMark = ($rightAnswersCount * 100) / $totalTestCount;
$update['summary_mark.$.primitives'][$primKey]['mark'] = $primitiveTargetMark;
where I am performing some logic then run this query:
MyClass::raw()
->findOneAndUpdate([
'course' => $this->first()->course,
'summary_mark.target_id' => $data->target_id,
],
['$set' => $update]);
the input is something like this:
{"data":{ "target_id":"5aa27e77967c552b10ea725b","question":"5aa141b6c8af28381079e9c7", "answers":[{"primitive_value":"B","result":1,"elpassed_time":20,"distructor":""},{"primitive_value":"T","result":1,"elpassed_time":3,"distructor":""}]}}
what I expect to see is the records with primitives (B,T,H) but i get (B,T) cause those which they are updated i cause of the input