1. 程式人生 > >php7 安裝mongodb4.0.4的擴充套件以及操作mongodb的方法

php7 安裝mongodb4.0.4的擴充套件以及操作mongodb的方法

資料插入:

$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);