dongsou0083 2014-11-30 10:55
浏览 109

PHP mongodb更新嵌套数组有多个条件

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" : "aasdasda@sdfsd.com", "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 ?

  • 写回答

1条回答 默认 最新

  • dougu4027 2014-12-08 19:49
    关注

    You need to use $elemMatch to get the array marker to be in the right place. So your $query becomes

    $condition = array( "session_id"=>'1111',
    "items" => array(
    '$elemMatch'=>array("itemId"=>126260, 'accessDays'=>30)));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助