数据插入:
$manager = new MongoDB\Driver\Manager(‘mongodb://localhost:27017‘); $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert([‘name‘ => ‘JetWu5‘, ‘age‘ => 26]); $bulk->insert([‘name‘ => ‘JetWu6‘, ‘age‘ => 26]); $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‘); $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);
原文地址:https://www.cnblogs.com/saintdingspage/p/10029996.html
时间: 2024-10-18 09:14:49