`
alfred_long
  • 浏览: 559607 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

php Mongodb扩展使用

    博客分类:
  • PHP
阅读更多

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.org

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics