Mongo.php
18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\db;
use Exception;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\AuthenticationException;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Driver\Exception\ConnectionException;
use MongoDB\Driver\Exception\InvalidArgumentException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Query as MongoQuery;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use think\db\connector\Mongo as MongoConnection;
use think\db\Query;
class Mongo extends Query
{
/**
* 架构函数
* @access public
*/
public function __construct(MongoConnection $connection = null)
{
if (is_null($connection)) {
$this->connection = MongoConnection::instance();
} else {
$this->connection = $connection;
}
$this->prefix = $this->connection->getConfig('prefix');
}
/**
* 去除某个查询条件
* @access public
* @param string $field 查询字段
* @param string $logic 查询逻辑 and or xor
* @return $this
*/
public function removeWhereField($field, $logic = 'and')
{
$logic = '$' . strtoupper($logic);
if (isset($this->options['where'][$logic])) {
foreach ($this->options['where'][$logic] as $key => $val) {
if (is_array($val) && $val[0] == $field) {
unset($this->options['where'][$logic][$key]);
}
}
}
return $this;
}
/**
* 执行查询 返回数据集
* @access public
* @param string $namespace
* @param MongoQuery $query 查询对象
* @param ReadPreference $readPreference readPreference
* @param bool|string $class 指定返回的数据集对象
* @param string|array $typeMap 指定返回的typeMap
* @return mixed
* @throws AuthenticationException
* @throws InvalidArgumentException
* @throws ConnectionException
* @throws RuntimeException
*/
public function mongoQuery($namespace, MongoQuery $query, ReadPreference $readPreference = null, $class = false, $typeMap = null)
{
return $this->connection->query($namespace, $query, $readPreference, $class, $typeMap);
}
/**
* 执行指令 返回数据集
* @access public
* @param Command $command 指令
* @param string $dbName
* @param ReadPreference $readPreference readPreference
* @param bool|string $class 指定返回的数据集对象
* @param string|array $typeMap 指定返回的typeMap
* @return mixed
* @throws AuthenticationException
* @throws InvalidArgumentException
* @throws ConnectionException
* @throws RuntimeException
*/
public function command(Command $command, $dbName = '', ReadPreference $readPreference = null, $class = false, $typeMap = null)
{
return $this->connection->command($command, $dbName, $readPreference, $class, $typeMap);
}
/**
* 执行语句
* @access public
* @param string $namespace
* @param BulkWrite $bulk
* @param WriteConcern $writeConcern
* @return int
* @throws AuthenticationException
* @throws InvalidArgumentException
* @throws ConnectionException
* @throws RuntimeException
* @throws BulkWriteException
*/
public function mongoExecute($namespace, BulkWrite $bulk, WriteConcern $writeConcern = null)
{
return $this->connection->execute($namespace, $bulk, $writeConcern);
}
/**
* 执行command
* @access public
* @param string|array|object $command 指令
* @param mixed $extra 额外参数
* @param string $db 数据库名
* @return array
*/
public function cmd($command, $extra = null, $db = null)
{
return $this->connection->cmd($this, $command, $extra, $db);
}
/**
* 指定distinct查询
* @access public
* @param string $field 字段名
* @return array
*/
public function distinct($field)
{
$result = $this->cmd('distinct', $field);
return $result[0]['values'];
}
/**
* 获取数据库的所有collection
* @access public
* @param string $db 数据库名称 留空为当前数据库
* @throws Exception
*/
public function listCollections($db = '')
{
$cursor = $this->cmd('listCollections', null, $db);
$result = [];
foreach ($cursor as $collection) {
$result[] = $collection['name'];
}
return $result;
}
/**
* COUNT查询
* @access public
* @return integer
*/
public function count($field = null)
{
$this->parseOptions();
$result = $this->cmd('count');
return $result[0]['n'];
}
/**
* 聚合查询
* @access public
* @param string $aggregate 聚合指令
* @param string $field 字段名
* @param bool $force 强制转为数字类型
* @return mixed
*/
public function aggregate($aggregate, $field, $force = false)
{
$this->parseOptions();
$result = $this->cmd('aggregate', [strtolower($aggregate), $field]);
$value = isset($result[0]['aggregate']) ? $result[0]['aggregate'] : 0;
if ($force) {
$value += 0;
}
return $value;
}
/**
* 多聚合操作
*
* @param array $aggregate 聚合指令, 可以聚合多个参数, 如 ['sum' => 'field1', 'avg' => 'field2']
* @param array $groupBy 类似mysql里面的group字段, 可以传入多个字段, 如 ['field_a', 'field_b', 'field_c']
* @return array 查询结果
*/
public function multiAggregate($aggregate, $groupBy)
{
$this->parseOptions();
$result = $this->cmd('multiAggregate', [$aggregate, $groupBy]);
foreach ($result as &$row) {
if (isset($row['_id']) && !empty($row['_id'])) {
foreach ($row['_id'] as $k => $v) {
$row[$k] = $v;
}
unset($row['_id']);
}
}
return $result;
}
/**
* 字段值(延迟)增长
* @access public
* @param string $field 字段名
* @param integer $step 增长值
* @param integer $lazyTime 延时时间(s)
* @return integer|true
* @throws Exception
*/
public function setInc($field, $step = 1, $lazyTime = 0)
{
$condition = !empty($this->options['where']) ? $this->options['where'] : [];
if (empty($condition)) {
// 没有条件不做任何更新
throw new Exception('no data to update');
}
if ($lazyTime > 0) {
// 延迟写入
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite($guid, $step, $lazyTime);
if (empty($step)) {
return true; // 等待下次写入
}
}
return $this->setField($field, ['$inc', $step]);
}
/**
* 字段值(延迟)减少
* @access public
* @param string $field 字段名
* @param integer $step 减少值
* @param integer $lazyTime 延时时间(s)
* @return integer|true
* @throws Exception
*/
public function setDec($field, $step = 1, $lazyTime = 0)
{
$condition = !empty($this->options['where']) ? $this->options['where'] : [];
if (empty($condition)) {
// 没有条件不做任何更新
throw new Exception('no data to update');
}
if ($lazyTime > 0) {
// 延迟写入
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite($guid, -$step, $lazyTime);
if (empty($step)) {
return true; // 等待下次写入
}
}
return $this->setField($field, ['$inc', -1 * $step]);
}
/**
* 字段值增长
* @access public
* @param string|array $field 字段名
* @param integer $step 增长值
* @return $this
*/
public function inc($field, $step = 1, $op = 'inc')
{
return parent::inc($field, $step, strtolower('$' . $op));
}
/**
* 字段值减少
* @access public
* @param string|array $field 字段名
* @param integer $step 减少值
* @return $this
*/
public function dec($field, $step = 1)
{
return $this->inc($field, -1 * $step);
}
/**
* 指定当前操作的collection
* @access public
* @param string $collection
* @return $this
*/
public function collection($collection)
{
return $this->table($collection);
}
/**
* 不主动获取数据集
* @access public
* @param bool $cursor 是否返回 Cursor 对象
* @return $this
*/
public function fetchCursor($cursor = true)
{
$this->options['fetch_cursor'] = $cursor;
return $this;
}
/**
* 设置typeMap
* @access public
* @param string|array $typeMap
* @return $this
*/
public function typeMap($typeMap)
{
$this->options['typeMap'] = $typeMap;
return $this;
}
/**
* awaitData
* @access public
* @param bool $awaitData
* @return $this
*/
public function awaitData($awaitData)
{
$this->options['awaitData'] = $awaitData;
return $this;
}
/**
* batchSize
* @access public
* @param integer $batchSize
* @return $this
*/
public function batchSize($batchSize)
{
$this->options['batchSize'] = $batchSize;
return $this;
}
/**
* exhaust
* @access public
* @param bool $exhaust
* @return $this
*/
public function exhaust($exhaust)
{
$this->options['exhaust'] = $exhaust;
return $this;
}
/**
* 设置modifiers
* @access public
* @param array $modifiers
* @return $this
*/
public function modifiers($modifiers)
{
$this->options['modifiers'] = $modifiers;
return $this;
}
/**
* 设置noCursorTimeout
* @access public
* @param bool $noCursorTimeout
* @return $this
*/
public function noCursorTimeout($noCursorTimeout)
{
$this->options['noCursorTimeout'] = $noCursorTimeout;
return $this;
}
/**
* 设置oplogReplay
* @access public
* @param bool $oplogReplay
* @return $this
*/
public function oplogReplay($oplogReplay)
{
$this->options['oplogReplay'] = $oplogReplay;
return $this;
}
/**
* 设置partial
* @access public
* @param bool $partial
* @return $this
*/
public function partial($partial)
{
$this->options['partial'] = $partial;
return $this;
}
/**
* maxTimeMS
* @access public
* @param string $maxTimeMS
* @return $this
*/
public function maxTimeMS($maxTimeMS)
{
$this->options['maxTimeMS'] = $maxTimeMS;
return $this;
}
/**
* collation
* @access public
* @param array $collation
* @return $this
*/
public function collation($collation)
{
$this->options['collation'] = $collation;
return $this;
}
/**
* 设置返回字段
* @access public
* @param array $field
* @param boolean $except 是否排除
* @return $this
*/
public function field($field, $except = false, $tableName = '', $prefix = '', $alias = '')
{
if (empty($field)) {
return $this;
} elseif ($field instanceof Expression) {
$this->options['field'][] = $field;
return $this;
}
if (is_string($field)) {
if (preg_match('/[\<\'\"\(]/', $field)) {
return $this->fieldRaw($field);
}
$field = array_map('trim', explode(',', $field));
}
$projection = [];
foreach ($field as $key => $val) {
if (is_numeric($key)) {
$projection[$val] = $except ? 0 : 1;
} else {
$projection[$key] = $val;
}
}
$this->options['projection'] = $projection;
return $this;
}
/**
* 设置skip
* @access public
* @param integer $skip
* @return $this
*/
public function skip($skip)
{
$this->options['skip'] = intval($skip);
return $this;
}
/**
* 设置slaveOk
* @access public
* @param bool $slaveOk
* @return $this
*/
public function slaveOk($slaveOk)
{
$this->options['slaveOk'] = $slaveOk;
return $this;
}
/**
* 指定查询数量
* @access public
* @param mixed $offset 起始位置
* @param mixed $length 查询数量
* @return $this
*/
public function limit($offset, $length = null)
{
if (is_null($length)) {
if (is_numeric($offset)) {
$length = $offset;
$offset = 0;
} else {
list($offset, $length) = explode(',', $offset);
}
}
$this->options['skip'] = intval($offset);
$this->options['limit'] = intval($length);
return $this;
}
/**
* 设置sort
* @access public
* @param array|string|object $field
* @param string $order
* @return $this
*/
public function order($field, $order = '')
{
if (is_array($field)) {
$this->options['sort'] = $field;
} else {
$this->options['sort'][$field] = 'asc' == strtolower($order) ? 1 : -1;
}
return $this;
}
/**
* 设置tailable
* @access public
* @param bool $tailable
* @return $this
*/
public function tailable($tailable)
{
$this->options['tailable'] = $tailable;
return $this;
}
/**
* 设置writeConcern对象
* @access public
* @param WriteConcern $writeConcern
* @return $this
*/
public function writeConcern($writeConcern)
{
$this->options['writeConcern'] = $writeConcern;
return $this;
}
/**
* 把主键值转换为查询条件 支持复合主键
* @access public
* @param array|string $data 主键数据
* @param mixed $options 表达式参数
* @return void
* @throws Exception
*/
public function parsePkWhere($data)
{
$pk = $this->getPk();
if (is_string($pk)) {
// 根据主键查询
if (is_array($data)) {
$where[$pk] = isset($data[$pk]) ? [$pk, '=', $data[$pk]] : [$pk, 'in', $data];
} else {
$where[$pk] = strpos($data, ',') ? [$pk, 'IN', $data] : [$pk, '=', $data];
}
}
if (!empty($where)) {
if (isset($this->options['where']['$and'])) {
$this->options['where']['$and'] = array_merge($this->options['where']['$and'], $where);
} else {
$this->options['where']['$and'] = $where;
}
}
return;
}
/**
* 获取当前数据表的主键
* @access public
* @param string|array $options 数据表名或者查询参数
* @return string|array
*/
public function getPk($options = '')
{
return $this->pk ?: $this->connection->getConfig('pk');
}
/**
* 执行查询但只返回Cursor对象
* @access public
* @return Cursor
*/
public function getCursor()
{
$this->parseOptions();
return $this->connection->getCursor($this);
}
/**
* 获取模型的更新条件
* @access protected
* @param array $options 查询参数
*/
protected function getModelUpdateCondition(array $options)
{
return isset($options['where']['$and']) ? $options['where']['$and'] : null;
}
/**
* 分析表达式(可用于查询或者写入操作)
* @access protected
* @return array
*/
protected function parseOptions()
{
$options = $this->options;
// 获取数据表
if (empty($options['table'])) {
$options['table'] = $this->getTable();
}
foreach (['where', 'data'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
}
$modifiers = empty($options['modifiers']) ? [] : $options['modifiers'];
if (isset($options['comment'])) {
$modifiers['$comment'] = $options['comment'];
}
if (isset($options['maxTimeMS'])) {
$modifiers['$maxTimeMS'] = $options['maxTimeMS'];
}
if (!empty($modifiers)) {
$options['modifiers'] = $modifiers;
}
if (!isset($options['projection']) || '*' == $options['projection']) {
$options['projection'] = [];
}
if (!isset($options['typeMap'])) {
$options['typeMap'] = $this->getConfig('type_map');
}
if (!isset($options['limit'])) {
$options['limit'] = 0;
}
foreach (['master', 'fetch_cursor'] as $name) {
if (!isset($options[$name])) {
$options[$name] = false;
}
}
if (isset($options['page'])) {
// 根据页数计算limit
list($page, $listRows) = $options['page'];
$page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['skip'] = intval($offset);
$options['limit'] = intval($listRows);
}
$this->options = $options;
return $options;
}
}