PHP操作MongoDB实现增删改查功能【附php7操作Mongo(4)

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017'); $query = new MongoDB\Driver\Query(['age' => 24], ['sort' => ['age' => 1]]); $cursor = $manager->executeQuery('wjt.friend', $query); $data = []; foreach($cursor as $doc) { $data[] = $doc; } echo '<pre>'; print_r($data);

数据修改:

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017'); $bulk = new MongoDB\Driver\BulkWrite; $bulk->update( ['name' => 'JetWu5'], ['$set' => ['age' => 30, 'promise' => 'always smile!']] ); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认 $res = $manager->executeBulkWrite('wjt.friend', $bulk, $writeConcern); echo '<pre>'; print_r($res);

数据删除:

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017'); $bulk = new MongoDB\Driver\BulkWrite; $bulk->delete(['name' => 'JetWu3']); $bulk->delete(['name' => 'JetWu4']); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认 $res = $manager->executeBulkWrite('wjt.friend', $bulk, $writeConcern); echo '<pre>'; print_r($res);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MongoDB数据库操作技巧大全》、《PHP基于pdo操作数据库技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8d1b4236859816ce2b9de76fe8c535a1.html