How to use findAndModify in php and mongodb

I want to increase id by 1, but I have a problem running php page Error

Fatal error: Call to undefined method MongoCollection::findAndModify() in C:\wamp\www\....

My code:

 // connect
 $m = new Mongo();
 $db=$m->demo;//selecting database named demo
 $db->authenticate("abc","abc");//authenticate database by its username and  password
 $next =nextValue($db);
 $db->counters.insert(array("_id"=>$next, "name"=>'B'));
 function nextValue($db)
      //$next =$db->counters->findAndModify(array('query'=> array("_id"=> "total"),'update'=>array($inc=> array("total"=> 1))));
      //I Used above Code Before this code
      $next =$db->command(array('findAndModify'=>'counters'),array('query'=> array("_id"=> "total"),'update'=>array($inc=> array("total"=> 1))));
         $db->counters->insert(array("_id"=> "total", "name" => 'A'));
         $next =$db->counters->findAndModify(array('query'=> array("_id"=> "total"),'update'=>array($inc=> array("total"=> 1))));
      return $next['total'];



source to share

1 answer

There is no findAndModify function yet. Instead of findAndModify, you will need to run a generic database command to do this:

    "findandmodify" => "counters",
    "query" => array("_id"=> "total"),
    "update" => array($inc=> array("total"=> 1)),


There is an open question at for the findAndModify implementation.



All Articles