Skip to content

Commit 2af4656

Browse files
committed
改进Mongo驱动 支持3.5+版本聚合查询
增加多聚合查询支持 修正事务问题
1 parent a672147 commit 2af4656

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

src/db/Mongo.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function aggregate($aggregate, $field, $force = false)
182182
$this->parseOptions();
183183

184184
$result = $this->cmd('aggregate', [strtolower($aggregate), $field]);
185-
$value = isset($result[0]['result'][0]['aggregate']) ? $result[0]['result'][0]['aggregate'] : 0;
185+
$value = isset($result[0]['aggregate']) ? $result[0]['aggregate'] : 0;
186186

187187
if ($force) {
188188
$value += 0;
@@ -191,6 +191,31 @@ public function aggregate($aggregate, $field, $force = false)
191191
return $value;
192192
}
193193

194+
/**
195+
* 多聚合操作
196+
*
197+
* @param array $aggregate 聚合指令, 可以聚合多个参数, 如 ['sum' => 'field1', 'avg' => 'field2']
198+
* @param array $groupBy 类似mysql里面的group字段, 可以传入多个字段, 如 ['field_a', 'field_b', 'field_c']
199+
* @return array 查询结果
200+
*/
201+
public function multiAggregate($aggregate, $groupBy)
202+
{
203+
$this->parseOptions();
204+
205+
$result = $this->cmd('multiAggregate', [$aggregate, $groupBy]);
206+
207+
foreach ($result as &$row) {
208+
if (isset($row['_id']) && !empty($row['_id'])) {
209+
foreach ($row['_id'] as $k => $v) {
210+
$row[$k] = $v;
211+
}
212+
unset($row['_id']);
213+
}
214+
}
215+
216+
return $result;
217+
}
218+
194219
/**
195220
* 字段值(延迟)增长
196221
* @access public

src/db/builder/Mongo.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ public function aggregate(Query $query, $extra)
535535
'aggregate' => $options['table'],
536536
'allowDiskUse' => true,
537537
'pipeline' => $pipeline,
538+
'cursor' => new \stdClass,
538539
];
539540

540541
foreach (['explain', 'collation', 'bypassDocumentValidation', 'readConcern'] as $option) {
@@ -550,6 +551,47 @@ public function aggregate(Query $query, $extra)
550551
return $command;
551552
}
552553

554+
/**
555+
* 多聚合查询命令, 可以对多个字段进行 group by 操作
556+
*
557+
* @param array $options 参数
558+
* @param array $extra 指令和字段
559+
* @return Command
560+
*/
561+
public function multiAggregate(Query $query, $extra)
562+
{
563+
$options = $query->getOptions();
564+
565+
list($aggregate, $groupBy) = $extra;
566+
$groups = ['_id' => []];
567+
foreach ($groupBy as $field) {
568+
$groups['_id'][$field] = '$' . $field;
569+
}
570+
571+
foreach ($aggregate as $fun => $field) {
572+
$groups[$field . '_' . $fun] = ['$' . $fun => '$' . $field];
573+
}
574+
$pipeline = [
575+
['$match' => (object) $this->parseWhere($query, $options['where'])],
576+
['$group' => $groups],
577+
];
578+
$cmd = [
579+
'aggregate' => $options['table'],
580+
'allowDiskUse' => true,
581+
'pipeline' => $pipeline,
582+
'cursor' => new \stdClass,
583+
];
584+
585+
foreach (['explain', 'collation', 'bypassDocumentValidation', 'readConcern'] as $option) {
586+
if (isset($options[$option])) {
587+
$cmd[$option] = $options[$option];
588+
}
589+
}
590+
$command = new Command($cmd);
591+
$this->log('group', $cmd);
592+
return $command;
593+
}
594+
553595
/**
554596
* 生成distinct命令
555597
* @access public

src/db/connector/Mongo.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ public function parseSqlTable($sql)
279279
return $sql;
280280
}
281281

282+
/**
283+
* 启动事务
284+
* @access public
285+
* @return void
286+
* @throws \PDOException
287+
* @throws \Exception
288+
*/
289+
public function startTrans()
290+
{}
291+
292+
/**
293+
* 用于非自动提交状态下面的查询提交
294+
* @access public
295+
* @return void
296+
* @throws PDOException
297+
*/
298+
public function commit()
299+
{}
300+
301+
/**
302+
* 事务回滚
303+
* @access public
304+
* @return void
305+
* @throws PDOException
306+
*/
307+
public function rollback()
308+
{}
309+
282310
/**
283311
* 执行查询
284312
* @access public

0 commit comments

Comments
 (0)