点开工具、字典、anything


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-&gt;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-&gt;mongoManage-&gt;executeCommand({database}, $commandObj); return json_decode(json_encode($cursor-&gt;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' =&gt; 'userinfo', 'pipeline' =&gt; [ [ '$project' =&gt; [ //筛选字段 'name' =&gt; 1, 'score' =&gt; 1 ] ], [ '$match' =&gt; [ 'name' =&gt; ['$regex' =&gt; 'aaa'], //$regex:模糊匹配 'created_at' =&gt; ['$gt' =&gt; 1562395000], //$gt:大于;$lt:小于;$gte:大于等于;$lte:小于等于 '$or' =&gt; [ //or 'name' =&gt; 'bbb' ] ] ], [ '$unwind' =&gt; '$name' //当使用group时,配合做字段筛选(可以忽略) ], [ '$group' =&gt; [ '_id' =&gt; '$name', //groupby $msg 'total' =&gt; ['$sum' =&gt; 1] //计算方法。$sum:总和;$avg:平均值;$max:最大值...... ] ], [ '$sort' =&gt; [ 'total' =&gt; -1 //排序。1:正序;-1:逆序 ] ], [ '$limit' =&gt; 10 //限制数量 ], ], 'cursor' =&gt; [ 'batchSize' =&gt; 0 //处理游标,默认使用0 ], 'hint' =&gt; [ 'type' =&gt; 1 //以type索引进行查询 ] 'allowDiskUse' =&gt; 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&gt;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' =&gt; 'userinfo', 'query' =&gt; [ 'created_at' =&gt; [ '$gt' =&gt; 1562395000 ], 'name' =&gt; [ '$regex' =&gt; 'aaa' ] ], 'limit' =&gt; 100, 'skip' =&gt; 10, 'hint' =&gt; [ 'type' =&gt; 1 //以type索引进行查询 ] ]; //或 $command = [ 'aggregate' =&gt; 'userinfo', '$project' =&gt; [ '_id' =&gt; 0 ], '$match' =&gt; [ 'created_at' =&gt; [ '$gt' =&gt; 1562395000 ], 'name' =&gt; [ '$regex' =&gt; 'aaa' ] ], '$group' =&gt; [ '_id' =&gt; null, 'count' =&gt; [ '$sum' =&gt; 1 ] ], 'limit' =&gt; 100, 'skip' =&gt; 10, 'hint' =&gt; [ 'type' =&gt; 1 //将type作为索引进行搜索 ] ]; </code></pre> <p><strong>count解析</strong></p> <pre><code class="language-php">SELECT count(*) AS count FROM userinfo WHERE created_at&gt;1562395000 AND name LIKE '%aaa%' OFFSET 10 LIMIT 100;</code></pre>

页面列表

ITEM_HTML