Answer the question
In order to leave comments, you need to log in
How to generate a PHP query to update data in mongoDB with conditions?
Hello,
I "stopped" writing this query:
The query increases the value of the 'countOperation' field and, based on the
new value of 'countOperation', it is necessary to calculate the 'sum' field
PHP 7.3
mongoDB 4.4.6
$id="123";
$clientName="Alex Petrov";
$price1=10;
$price2=11;
$price3=12;
$price4=13;
$res= $db->collection->findOneAndUpdate(
array('_id'=>$id),
array(
'$inc' => array('countOperations'=>1),
'$setOnInsert' => array(
'clientname'=>$clientName
),
'$set' => array(
'sum'=>array(
// псевдокод 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;
}
// как вариант можно использовать
// switch()
// { case: then:
// defaul:
// }
// псевдокод end
)
)
),
array('upsert'=>true)
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question