php mongodb manager 查數據的各種姿勢
阿新 • • 發佈:2017-12-28
col mage em1 sku god gre 條件 圖片 []
一、連接 mongodb 數據庫
以下操作默認都是以上面操作為前提(已連接成功)而做的操作
二、查的各種姿勢
1)通過主鍵_id來查詢,註意:_id不是直接用字符串來表示,要用 new \MongoDB\BSON\ObjectId 對象
// mysql: SELECT * FROM appDownloadRecord WHERE _id=‘5a2b9aaf96b4a97efbe31a91‘; $id = new \MongoDB\BSON\ObjectId("5a2b9aaf96b4a97efbe31a91"); $filter = [‘_id‘ => $id];$options = []; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
2)查詢指定字段(其中_id默認是返回的,如果不想返回,要設置 _id=>0)
// mysql: SELECT `source`,`ip` FROM appDownloadRecord WHERE _id=‘5a2b9aaf96b4a97efbe31a91‘;$id = new \MongoDB\BSON\ObjectId("5a2b9aaf96b4a97efbe31a91"); $filter = [‘_id‘ => $id]; $options = [ ‘projection‘ => [‘_id‘=>0, ‘source‘=>1, ‘ip‘=>1] ]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
3)普通條件查詢
// mysql: SELECT * FROM appDownloadRecord WHERE source=‘bdsem1‘ LIMIT 10; $filter = [‘source‘ => ‘bdsem1‘]; $options = [‘limit‘=>10]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
4)範圍查詢(between)
// mysql: SELECT * FROM appDownloadRecord WHERE (source=‘bdsem1‘) AND (createTime BEWTEEN ‘2017-12-21 00:00:00‘ AND ‘2017-12-28 00:00:00‘) LIMIT 10; // mysql: SELECT * FROM appDownloadRecord WHERE (source=‘bdsem1‘) AND (createTime>=‘2017-12-21 00:00:00‘ AND createTime<‘2017-12-28 00:00:00‘) LIMIT 10; $startTime = new \MongoDB\BSON\UTCDateTime(strtotime(‘2017-12-21 00:00:00‘)*1000); $endTime = new \MongoDB\BSON\UTCDateTime(strtotime(‘2017-12-28 00:00:00‘)*1000); $filter = [ ‘source‘ => ‘bdsem1‘, ‘createTime‘ => [ ‘$gte‘ => $startTime, ‘$lt‘ => $endTime, ], ]; $options = [‘limit‘=>10]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
5)in 查詢
// mysql: SELECT * FROM appDownloadRecord WHERE (source IN (‘bdsem1‘, ‘besem2‘, ‘besem3‘)) LIMIT 10; $filter = [ ‘source‘ => [ ‘$in‘ => [‘bdsem1‘, ‘bdsem2‘, ‘bdsem3‘] ], ]; $options = [‘limit‘=>10]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
6)or 操作
// mysql: SELECT * FROM appDownloadRecord WHERE source=‘bdsem1‘ AND (sys=‘ios‘ OR sys=‘other‘) LIMIT 10; $filter = [ ‘source‘ => ‘bdsem1‘, ‘$or‘ => [ [‘sys‘=>‘ios‘], [‘sys‘=>‘other‘] ] ]; $options = [‘limit‘=>10]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
7)group (聚合)操作
// mysql: SELECT sys,sum(*) as total FROM appDownloadRecord WHERE source=‘bdsem1‘ GROUP BY sys; $document = [ ‘aggregate‘ => ‘appDownloadRecord‘, ‘pipeline‘ => [ [ ‘$match‘ => [ ‘source‘ => ‘bdsem1‘ ] ], [ ‘$group‘ => [ ‘_id‘ => ‘$sys‘, ‘total‘ => [‘$sum‘=>1] ] ] ], ‘allowDiskUse‘ => false, ]; $command = new \MongoDB\Driver\Command($document); $rows = $mongo->executeCommand(‘promotion-prod‘, $command)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
8)group having
// mysql: SELECT sys,sum(*) as total FROM appDownloadRecord WHERE source=‘bdsem1‘ GROUP BY sys HAVING total>1000; $document = [ ‘aggregate‘ => ‘appDownloadRecord‘, ‘pipeline‘ => [ [ ‘$match‘ => [ ‘source‘ => ‘bdsem1‘ ] ], [ ‘$group‘ => [ ‘_id‘ => ‘$sys‘, ‘total‘ => [‘$sum‘=>1] ] ], [ ‘$match‘ => [ ‘total‘ => [‘$gt‘=>1000] ] ] ], ‘allowDiskUse‘ => false, ]; $command = new \MongoDB\Driver\Command($document); $rows = $mongo->executeCommand(‘promotion-prod‘, $command)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
9)排序 sort
// mysql: SELECT * FROM appDownloadRecord WHERE source=‘bdsem1‘ ORDER BY createTime desc LIMIT 10; $filter = [‘source‘ => ‘bdsem1‘]; $options = [ ‘sort‘ => [‘createTime‘=>-1], ‘limit‘ => 10 ]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
10)limit 分頁
// mysql: SELECT * FROM appDownloadRecord WHERE source=‘bdsem1‘ LIMIT 10; $filter = [‘source‘ => ‘bdsem1‘]; $options = [ ‘limit‘ => 10 ]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
// mysql: SELECT * FROM appDownloadRecord WHERE source=‘bdsem1‘ LIMIT 10,10; $filter = [‘source‘ => ‘bdsem1‘]; $options = [ ‘skip‘ => 10, ‘limit‘ => 10 ]; $query = new \MongoDB\Driver\Query($filter, $options); $rows = $mongo->executeQuery(‘promotion-prod.appDownloadRecord‘, $query)->toArray(); echo ‘<pre>‘;print_r($rows);echo ‘</pre>‘;exit;
完。
php mongodb manager 查數據的各種姿勢