Why this update query only update one record once

前端 未结 3 1497
执笔经年
执笔经年 2021-01-19 06:17
$coll->update(
  array(
    \"uid\"=(int)$uid,
    \"status\"=>1,
    \"time\"=>array(\'$gt\'=>0,\'$lte\'=>$time)
  ),
  array(
    \'$set\'=>array         


        
相关标签:
3条回答
  • 2021-01-19 06:26

    Now MongoDB is using updateMany to update multiple documents: db.collection.updateMany(, , )

        db.collection.updateMany(criteria, objNew)
    
    0 讨论(0)
  • 2021-01-19 06:34

    Seeing as this was done in PHP initially, this may be of use to anyone using PHP:

    $collection->update(
    
        array("uid"=>(int)$uid,"status"=>1,"time"=>array('$gt'=>0,'$lte'=>$time)), //search criteria
    
        array('$set'=>array('status'=>0)), //update criteria
    
        array('multiple'=>true) //options
    
    );
    
    0 讨论(0)
  • 2021-01-19 06:40

    This is the MongoDB default behaviour for updates. If you want to update multiple documents at once, you'll explicitly have to provide the multi flag:

    db.collection.update( criteria, objNew, upsert, multi )
    

    so you'd have to use

    db.we.update({"uid":1, "status":1, "time" : {"$lte":1324403899}},
                 {"$set":{status:0}}, 
                 false, 
                 true);
    

    instead.

    From the documentation:

    If you are coming from SQL, be aware that by default, update() only modifies the first matched object. If you want to modify all matched objects, you need to use the multi flag.

    0 讨论(0)
提交回复
热议问题