2012-03-22 85 views
3

我想通過1遞增ID但在運行PHP的頁面,而我有一個問題 錯誤是如何在PHP中使用findAndModify和MongoDB

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']; 
} 
?> 

回答

4

還沒有findAndModify函數。相反findAndModify的,你必須運行通用數據庫命令做到這一點:

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

有一個在https://jira.mongodb.org/browse/PHP-117一個開放的問題,雖然實現findAndModify。

+4

只爲大家看着這個 - 輔助命令被添加到2012年8月:http://php.net/manual/en/mongocollection.findandmodify.php – Tobias 2012-12-13 00:17:59

相關問題