php7.0操作mongodb資料庫
阿新 • • 發佈:2018-12-23
<?php //-------------增刪改-------------------------- // 第一 $bulk = new MongoDB\Driver\BulkWrite; // 以下第二步可存在一個也可存在多個 // 第二 -- 增加 $bulk->insert(['_id' => new MongoDB\BSON\ObjectID, 'name' => '123']); // 第二 -- 刪除 $bulk->delete(['x' => 1], ['limit' => 1]); // limit 為 1 時,刪除第一條匹配資料 $bulk->delete(['x' => 2], ['limit' => 0]); // limit 為 0 時,刪除所有匹配資料 // 第二 -- 修改 $bulk->update( ['x' => 2], ['$set' => ['name' => '菜鳥工具', 'url' => 'tool.runoob.com']], ['multi' => false, 'upsert' => false] ); // 第三 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 第四 插入資料時可不填 $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); // 第五 $result = $manager->executeBulkWrite('test.runoob', $bulk, $writeConcern); // -------------查-------------------------- // 第一 $filter = ['x' => ['$gt' => 1]]; // 第二 $options = [ 'projection' => ['_id' => 0], 'sort' => ['x' => -1], ]; // 第三 $query = new MongoDB\Driver\Query($filter, $options); // 第四 $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); // 第五 $cursor = $manager->executeQuery('test.sites', $query); // 輸出查詢資料 foreach ($cursor as $document) { print_r($document); } ?>