2019-07-13
<h1>Mongo命令整理</h1>
<p><strong>初始化mongo连接</strong></p>
<pre><code class="language-php">/**
* 变量描述
* username-连接用户名
* password-连接密码
* port-端口
* database-数据库
**/
protect $mongoManage=null;
public function mongeInit()
{
$this->mongoManage = new \MongoDB\Driver\Manager("mongodb://" . {username} . ":" . {password} . "@" . {127.0.0.1} . ":" . {port} . "/" . {database});
}
command运行方法
public function command($command)
{
$commandObj = new \MongoDB\Driver\Command($command);
$cursor = $this->mongoManage->executeCommand({database}, $commandObj);
return json_decode(json_encode($cursor->toArray()), true);
}</code></pre>
<p><strong>实验数据示例</strong></p>
<pre><code class="language-php">collection:"userinfo"
{_id:1,name:"aaa",score:"100",type:"math",created_at:1562393000}
{_id:2,name:"bbb",score:"100",type:"en",created_at:1562394000}
{_id:3,name:"ccc",score:"100",type:"math",created_at:1562395000}
{_id:4,name:"aaa",score:"100",type:"en",created_at:1562396000}
{_id:5,name:"bbb",score:"100",type:"math",created_at:1562397000}</code></pre>
<p><strong>aggregate字段说明(聚合查询)</strong></p>
<pre><code class="language-php">$command = [
'aggregate' => 'userinfo',
'pipeline' => [
[
'$project' => [ //筛选字段
'name' => 1,
'score' => 1
]
],
[
'$match' => [
'name' => ['$regex' => 'aaa'], //$regex:模糊匹配
'created_at' => ['$gt' => 1562395000], //$gt:大于;$lt:小于;$gte:大于等于;$lte:小于等于
'$or' => [ //or
'name' => 'bbb'
]
]
],
[
'$unwind' => '$name' //当使用group时,配合做字段筛选(可以忽略)
],
[
'$group' => [
'_id' => '$name', //groupby $msg
'total' => ['$sum' => 1] //计算方法。$sum:总和;$avg:平均值;$max:最大值......
]
],
[
'$sort' => [
'total' => -1 //排序。1:正序;-1:逆序
]
],
[
'$limit' => 10 //限制数量
],
],
'cursor' => [
'batchSize' => 0 //处理游标,默认使用0
],
'hint' => [
'type' => 1 //以type索引进行查询
]
'allowDiskUse' => true, //当查询数据量过大时,开启磁盘辅助查询
];</code></pre>
<p><strong>aggregate使用解析</strong></p>
<pre><code class="language-php">//相当于sql
SELECT name AS _id, count(*) AS total FROM userinfo WHERE name LIKE '%aaa%' AND created_at>1562395000 OR name='bbb' GROUP BY name ORDER BY total DESC LIMIT 10;</code></pre>
<p><strong>aggregate在shell中使用</strong></p>
<pre><code class="language-php">db.userinfo.aggregate({$match:{name:{$regex:"aaa"}},{created_at:{$gt:1562395000}},{$or:{name:"bbb"}}},{$unwind:"$name"},{$group:{_id:"$name",total:{$sum:1}}},{$sort:{total:-1}},{$limit:10},{cursor:{batchSize:0}},{allowDiskUse:true})</code></pre>
<p><strong>count组成</strong></p>
<pre><code class="language-php">$command = [
'count' => 'userinfo',
'query' => [
'created_at' => [
'$gt' => 1562395000
],
'name' => [
'$regex' => 'aaa'
]
],
'limit' => 100,
'skip' => 10,
'hint' => [
'type' => 1 //以type索引进行查询
]
];
//或
$command = [
'aggregate' => 'userinfo',
'$project' => [
'_id' => 0
],
'$match' => [
'created_at' => [
'$gt' => 1562395000
],
'name' => [
'$regex' => 'aaa'
]
],
'$group' => [
'_id' => null,
'count' => [
'$sum' => 1
]
],
'limit' => 100,
'skip' => 10,
'hint' => [
'type' => 1 //将type作为索引进行搜索
]
];
</code></pre>
<p><strong>count解析</strong></p>
<pre><code class="language-php">SELECT count(*) AS count FROM userinfo WHERE created_at>1562395000 AND name LIKE '%aaa%' OFFSET 10 LIMIT 100;</code></pre>