我想将id增加1,但运行php页面时出现问题
错误是
Fatal error: Call to undefined method MongoCollection::findAndModify() in C:\wamp\www\....
我的代码是:

<?php
 // 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'));
 print_r($db->runcommand(array('getlasterror'=>1,'fsync'=>true)));
 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))));
      if($next['total']==0)
      {
         $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'];
 }
?>

最佳答案

还没有findAndModify函数。要执行此操作,必须运行通用数据库命令,而不是find和modify:

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

但是,在https://jira.mongodb.org/browse/PHP-117处有一个开放的问题要实现find和modify。

10-08 06:50