本文介绍了如何在PHP和MongoDB中使用findAndModify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将id递增1,但是运行php页面时出现问题 错误是
I want to increment id by 1 but I there is a problem while running the php page The Error is
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函数.代替findAndModify,您必须运行通用数据库命令来执行此操作:
There is no findAndModify function yet. Instead of findAndModify, you'll have to run a generic database command to do this:
$db->command(
array(
"findandmodify" => "counters",
"query" => array("_id"=> "total"),
"update" => array($inc=> array("total"=> 1)),
)
);
在 https://jira.mongodb.org/browse/PHP- 117 来实现findAndModify.
There is an open issue at https://jira.mongodb.org/browse/PHP-117 to implement findAndModify though.
这篇关于如何在PHP和MongoDB中使用findAndModify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!