dongxun2089 2017-03-28 12:51
浏览 81
已采纳

php mongodb findandmodify不工作

I am using php and mongodb. I want to use findandmodify. My field is { "_id" : ObjectId("58d37e612d4ffa498b99c2d4"), "userid" : "1234", "active_time" : "hai" }

I want to modify active_time. For example change the value "hai" to "1234"

I am using MongoDB\Driver\Manager.

try {
        $mng = new MongoDB\Driver\Manager("mongodb://username:password@localhost:27017/db");
        $userid = '1234';
        $retval = $mng->findAndModify(
        array("userid" => $userid), // searchQuery
        array('$set' => array('active_time' => "kkk")) // UpdateQuery
        );
        $command = new MongoDB\Driver\Command($retval);
        $cursor = $manager->executeCommand('db.online', $command);
} catch (MongoDB\Driver\Exception\Exception $e) {
    $filename = basename(__FILE__);
    echo "The $filename script has experienced an error.
"; 
    echo "It failed with the following exception:
";
    echo "Exception:", $e->getMessage(), "
";
    echo "In file:", $e->getFile(), "
";
    echo "On line:", $e->getLine(), "
";       
}

It shows the error is

Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Manager::findAndModify()

in line

$retval = $mng->findAndModify(
 array("userid" => $userid), // searchQuery
 array('$set' => array('active_time' => "kkk")) // UpdateQuery
 );

How it possible? please help me?

  • 写回答

1条回答 默认 最新

  • dongtuo3530 2017-03-28 13:08
    关注

    I got the answer. Change the findAndModify to update. The modified code is shown below.

    $bulk->update(
     array("userid" => '1234'), // searchQuery
     array('$set' => array('active_time' => "kkk")) // UpdateQuery
     );
    
     $mng->executeBulkWrite("browser.online", $bulk);
    
     if(!empty($mng)) {
        echo "success";
        } else {
            echo "not";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏