This request stopped me).
What the request should do:
-
Find a document by _id or create a new document if not found
-
Change the value of the 'count Operations' field (increment)
-
Calculate the 'sum' field using the incremented value of the 'count Operations' field and the comparison conditions (see block 'pseudocode')
-
Save document with new values
I stopped at the stage of calculating 'sum' ...
PHP 7.3
mongoDB 4.4.6
$id="123";
$clientName="Alex Petrov";
$num=8;
$price1=10;
$price2=11;
$price3=12;
$price4=13;
$res= $db->collection->findOneAndUpdate(
array('_id'=>$id),
array(
'$inc' => array('countOperations'=>$num),
'$setOnInsert' => array(
'clientname'=>$clientName
),
'$set' => array(
'sum'=>array(
////////////////////
// pseudocode begin
////////////////////
if('countOperations'>1 && 'countOperations'<=10)
{ 'sum'='countOperations'*$price1;
}
else
if('countOperations'>10 && 'countOperations'<=20)
{ 'sum'='countOperations'*$price2;
}
else
if('countOperations'>20 && 'countOperations'<=30)
{ 'sum'='countOperations'*$price3;
}
else
{ 'sum'='countOperations'*$price4;
}
// as an option I planned to use:
// switch()
// { case: then:
// defaul:
// }
////////////////////
// pseudocode end
////////////////////
)
)
),
array('upsert'=>true)
);
Aucun commentaire:
Enregistrer un commentaire