Home:ALL Converter>PHP mongodb update on nested array with multiple conditions

PHP mongodb update on nested array with multiple conditions

Ask Time:2014-11-30T18:55:18         Author:Anuj

Json Formatter

I've thoroughly searched on the internet for this but haven't found a solution.

Problem : I want to update the quantity in this document. Criteria - itemId=126260, accessDays=30

`{ "_id" : ObjectId("547acfa95ca86bec2e000029"), "session_id" : "1111", "email" : "[email protected]", "isProcessed" : 0, "couponApplied" : "", "countryId" : 2, "items" : [ { "itemId" : 126260, "batchId" : 102970, "accessDays" : null, "quantity" : 2 }, { "itemId" : 126260, "batchId" : null, "accessDays" : 30, "quantity" : 2 } ] }`

I am trying to do this using PHP :

`$condition = array( "session_id"=>'1111', 'items.itemId'=>126260, 'items.accessDays'=>30);
$new_values = array( '$set' => array("items.$.quantity" => 10) );

$cart_coll->update($condition, $new_values);`

But when I run this code, it updates the first nested object instead of the second.

What am I doing wrong here ? Does mongodb not consider multiple conditions in nested objects ?

Author:Anuj,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27212458/php-mongodb-update-on-nested-array-with-multiple-conditions
yy