php mongodb manager 查数据的各种姿势
Posted Tiac
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php mongodb manager 查数据的各种姿势相关的知识,希望对你有一定的参考价值。
一、连接 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 查数据的各种姿势的主要内容,如果未能解决你的问题,请参考以下文章