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";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度