php Mongodb擴展使用

mongodb官方推薦擴展

查找數據

$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$filter = [hid=>1];
$options = array(
    "projection" => array(
        "hid" => 1,
        "total" => 1,
    ),
    "sort" => array(
        "total" => -1,
    ),
    "modifiers" => array(
        '$comment'   => "This is a query comment",
        '$maxTimeMS' => 100,
    ),
    "skip" => 2,
    "limit" => 1,
);
$query = new MongoDB\Driver\Query($filter,$options);
$cursor = $manager->executeQuery("aka.test", $query);
var_dump($cursor->toArray());exit;

 

count統計

$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$commands = [ 
    'count' => "test", 
    'query' => [
        //'hid'=>['$eq'=>1]
        'hid'=>['$lt'=>3]
    ],  
];
$command = new MongoDB\Driver\Command($commands);
$cursor = $manager->executeCommand("aka", $command);
var_dump($cursor->toArray());exit;

 

聚合查詢

$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$date = '2016-01-11';
$startime = $date. ' 00:00:00';
$endtime = $date. ' 23:59:59';
$commands = [ 
    'aggregate' => "test", 
    'pipeline' =>[
        ['$match'=>['createtime'=>['$gte'=>$startime, '$lte' => $endtime]]],
        ['$group'=>['_id'=>'$hid','total'=>['$sum'=>'$total']]],
    ]
];
$command = new MongoDB\Driver\Command($commands);
try{
    $cursor = $manager->executeCommand("aka", $command);
}catch(Exception $e){
    //沒法鏈接數據庫 異常處理
    echo "MongoDB Connection Error";exit;
}
$response = $cursor->toArray()[0]->result;
foreach($response as $v) {
    echo $v->_id.':'.$v->total."\n";
}

 

數據插入|修改|刪除

  • 插入數據
$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);//順序or並行發送到服務器執行
$bulk->insert(
  ['id' => 3, 'total'=> 5]
);
$bulk->insert(
  ['id' => 4, 'total'=> 8]
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('aka.test', $bulk, $writeConcern);
var_dump($result);

 

  • 修改數據
$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);//順序or並行發送到服務器執行
$bulk->update(
  ['hid' => 3], 
  ['$set' => ['total' => 1000]],
  ['multi' => true, 'upsert' => true] //multi多個結果修改,upsert若是不存在就插入
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('aka.test', $bulk, $writeConcern);
var_dump($result);

 

  • 刪除數據
$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);//順序or並行發送到服務器執行
$bulk->delete(['hid'=>1]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('aka.test', $bulk, $writeConcern);
var_dump($result);

 

增刪改查能夠放在一塊兒

$connectString = 'mongodb://127.0.0.1:27017/aka';
$manager = new MongoDB\Driver\Manager($connectString);
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);//true順序 false 並行發送到服務器執行

$bulk->insert(
    ['hid' => 3, 'total'=> 5]
);
$bulk->insert(
    ['hid' => 4, 'total'=> 8]
);

$bulk->update(
    ['hid' => 3], 
    ['$set' => ['total' => 1000]],
    ['multi' => true, 'upsert' => true] //multi多個結果修改,upsert若是不存在就插入
);

$bulk->delete(['hid'=>1]);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('aka.test', $bulk, $writeConcern);
var_dump($result);

 

參考資料:
php.net
doc.mongodb.orgphp