forked from yiisoft/yii2-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
835 lines (736 loc) · 29.2 KB
/
Command.php
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongodb;
use MongoDB\BSON\ObjectID;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\WriteResult;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Object;
/**
* Command represents MongoDB statement such as command or query.
*
* A command object is usually created by calling [[Connection::createCommand()]] or [[Database::createCommand()]].
* The statement it represents can be set via the [[document]] property.
*
* To execute a non-query command, such as 'listIndexes', 'count', 'distinct' and so on, call [[execute()]].
* For example:
*
* ```php
* $result = Yii::$app->mongodb->createCommand(['listIndexes' => 'some_collection'])->execute();
* ```
*
* To execute a 'find' command, which return cursor, call [[query()]].
* For example:
*
* ```php
* $cursor = Yii::$app->mongodb->createCommand(['projection' => ['name' => true]])->query('some_collection');
* ```
*
* To execute batch (bulk) operations, call [[executeBatch()]].
* For example:
*
* ```php
* Yii::$app->mongodb->createCommand()
* ->addInsert(['name' => 'new'])
* ->addUpdate(['name' => 'existing'], ['name' => 'updated'])
* ->addDelete(['name' => 'old'])
* ->executeBatch('some_collection');
* ```
*
* @property ReadConcern|string $readConcern Read concern to be used in this command.
* @property ReadPreference $readPreference Read preference. Note that the type of this property differs in
* getter and setter. See [[getReadPreference()]] and [[setReadPreference()]] for details.
* @property WriteConcern|null $writeConcern Write concern to be used in this command. Note that the type of
* this property differs in getter and setter. See [[getWriteConcern()]] and [[setWriteConcern()]] for details.
*
* @author Paul Klimov <[email protected]>
* @since 2.1
*/
class Command extends Object
{
/**
* @var Connection the MongoDB connection that this command is associated with.
*/
public $db;
/**
* @var string name of the database that this command is associated with.
*/
public $databaseName;
/**
* @var array command document contents.
*/
public $document = [];
/**
* @var ReadPreference|int|string|null command read preference.
*/
private $_readPreference;
/**
* @var WriteConcern|int|string|null write concern to be used by this command.
*/
private $_writeConcern;
/**
* @var ReadConcern|string read concern to be used by this command
*/
private $_readConcern;
/**
* Returns read preference for this command.
* @return ReadPreference read preference.
*/
public function getReadPreference()
{
if (!is_object($this->_readPreference)) {
if ($this->_readPreference === null) {
$this->_readPreference = $this->db->manager->getReadPreference();
} elseif (is_scalar($this->_readPreference)) {
$this->_readPreference = new ReadPreference($this->_readPreference);
}
}
return $this->_readPreference;
}
/**
* Sets read preference for this command.
* @param ReadPreference|int|string|null $readPreference read reference, it can be specified as
* instance of [[ReadPreference]] or scalar mode value, for example: `ReadPreference::RP_PRIMARY`.
* @return $this self reference.
*/
public function setReadPreference($readPreference)
{
$this->_readPreference = $readPreference;
return $this;
}
/**
* Returns write concern for this command.
* @return WriteConcern|null write concern to be used in this command.
*/
public function getWriteConcern()
{
if ($this->_writeConcern !== null) {
if (is_scalar($this->_writeConcern)) {
$this->_writeConcern = new WriteConcern($this->_writeConcern);
}
}
return $this->_writeConcern;
}
/**
* Sets write concern for this command.
* @param WriteConcern|int|string|null $writeConcern write concern, it can be an instance of [[WriteConcern]]
* or its scalar mode value, for example: `majority`.
* @return $this self reference
*/
public function setWriteConcern($writeConcern)
{
$this->_writeConcern = $writeConcern;
return $this;
}
/**
* Retuns read concern for this command.
* @return ReadConcern|string read concern to be used in this command.
*/
public function getReadConcern()
{
if ($this->_readConcern !== null) {
if (is_scalar($this->_readConcern)) {
$this->_readConcern = new ReadConcern($this->_readConcern);
}
}
return $this->_readConcern;
}
/**
* Sets read concern for this command.
* @param ReadConcern|string $readConcern read concern, it can be an instance of [[ReadConcern]] or
* scalar level value, for example: 'local'.
* @return $this self reference
*/
public function setReadConcern($readConcern)
{
$this->_readConcern = $readConcern;
return $this;
}
/**
* Executes this command.
* @return \MongoDB\Driver\Cursor result cursor.
* @throws Exception on failure.
*/
public function execute()
{
$databaseName = $this->databaseName === null ? $this->db->defaultDatabaseName : $this->databaseName;
$token = $this->log([$databaseName, 'command'], $this->document, __METHOD__);
try {
$this->beginProfile($token, __METHOD__);
$this->db->open();
$mongoCommand = new \MongoDB\Driver\Command($this->document);
$cursor = $this->db->manager->executeCommand($databaseName, $mongoCommand, $this->getReadPreference());
$cursor->setTypeMap($this->db->typeMap);
$this->endProfile($token, __METHOD__);
} catch (RuntimeException $e) {
$this->endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
return $cursor;
}
/**
* Execute commands batch (bulk).
* @param string $collectionName collection name.
* @param array $options batch options.
* @return array array of 2 elements:
*
* - 'insertedIds' - contains inserted IDs.
* - 'result' - [[\MongoDB\Driver\WriteResult]] instance.
*
* @throws Exception on failure.
* @throws InvalidConfigException on invalid [[document]] format.
*/
public function executeBatch($collectionName, $options = [])
{
$databaseName = $this->databaseName === null ? $this->db->defaultDatabaseName : $this->databaseName;
$token = $this->log([$databaseName, $collectionName, 'bulkWrite'], $this->document, __METHOD__);
try {
$this->beginProfile($token, __METHOD__);
$batch = new BulkWrite($options);
$insertedIds = [];
foreach ($this->document as $key => $operation) {
switch ($operation['type']) {
case 'insert':
$insertedIds[$key] = $batch->insert($operation['document']);
break;
case 'update':
$batch->update($operation['condition'], $operation['document'], $operation['options']);
break;
case 'delete':
$batch->delete($operation['condition'], isset($operation['options']) ? $operation['options'] : []);
break;
default:
throw new InvalidConfigException("Unsupported batch operation type '{$operation['type']}'");
}
}
$this->db->open();
$writeResult = $this->db->manager->executeBulkWrite($databaseName . '.' . $collectionName, $batch, $this->getWriteConcern());
$this->endProfile($token, __METHOD__);
} catch (RuntimeException $e) {
$this->endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
return [
'insertedIds' => $insertedIds,
'result' => $writeResult,
];
}
/**
* Executes this command as a mongo query
* @param string $collectionName collection name
* @param array $options query options.
* @return \MongoDB\Driver\Cursor result cursor.
* @throws Exception on failure
*/
public function query($collectionName, $options = [])
{
$databaseName = $this->databaseName === null ? $this->db->defaultDatabaseName : $this->databaseName;
$token = $this->log(
'find',
array_merge(
[
'ns' => $databaseName . '.' . $collectionName,
'filter' => $this->document,
],
$options
),
__METHOD__
);
$readConcern = $this->getReadConcern();
if ($readConcern !== null) {
$options['readConcern'] = $readConcern;
}
try {
$this->beginProfile($token, __METHOD__);
$query = new \MongoDB\Driver\Query($this->document, $options);
$this->db->open();
$cursor = $this->db->manager->executeQuery($databaseName . '.' . $collectionName, $query, $this->getReadPreference());
$cursor->setTypeMap($this->db->typeMap);
$this->endProfile($token, __METHOD__);
} catch (RuntimeException $e) {
$this->endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
return $cursor;
}
/**
* Drops database associated with this command.
* @return bool whether operation was successful.
*/
public function dropDatabase()
{
$this->document = $this->db->getQueryBuilder()->dropDatabase();
$result = current($this->execute()->toArray());
return $result['ok'] > 0;
}
/**
* Creates new collection in database associated with this command.s
* @param string $collectionName collection name
* @param array $options collection options in format: "name" => "value"
* @return bool whether operation was successful.
*/
public function createCollection($collectionName, array $options = [])
{
$this->document = $this->db->getQueryBuilder()->createCollection($collectionName, $options);
$result = current($this->execute()->toArray());
return $result['ok'] > 0;
}
/**
* Drops specified collection.
* @param string $collectionName name of the collection to be dropped.
* @return bool whether operation was successful.
*/
public function dropCollection($collectionName)
{
$this->document = $this->db->getQueryBuilder()->dropCollection($collectionName);
$result = current($this->execute()->toArray());
return $result['ok'] > 0;
}
/**
* Creates indexes in the collection.
* @param string $collectionName collection name.
* @param array[] $indexes indexes specification. Each specification should be an array in format: optionName => value
* The main options are:
*
* - keys: array, column names with sort order, to be indexed. This option is mandatory.
* - unique: bool, whether to create unique index.
* - name: string, the name of the index, if not set it will be generated automatically.
* - background: bool, whether to bind index in the background.
* - sparse: bool, whether index should reference only documents with the specified field.
*
* See [[https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options-for-all-index-types]]
* for the full list of options.
* @return bool whether operation was successful.
*/
public function createIndexes($collectionName, $indexes)
{
$this->document = $this->db->getQueryBuilder()->createIndexes($this->databaseName, $collectionName, $indexes);
$result = current($this->execute()->toArray());
return $result['ok'] > 0;
}
/**
* Drops collection indexes by name.
* @param string $collectionName collection name.
* @param string $indexes wildcard for name of the indexes to be dropped.
* @return array result data.
*/
public function dropIndexes($collectionName, $indexes)
{
$this->document = $this->db->getQueryBuilder()->dropIndexes($collectionName, $indexes);
return current($this->execute()->toArray());
}
/**
* Returns information about current collection indexes.
* @param string $collectionName collection name
* @param array $options list of options in format: optionName => optionValue.
* @return array list of indexes info.
* @throws Exception on failure.
*/
public function listIndexes($collectionName, $options = [])
{
$this->document = $this->db->getQueryBuilder()->listIndexes($collectionName, $options);
try {
$cursor = $this->execute();
} catch (Exception $e) {
// The server may return an error if the collection does not exist.
$notFoundCodes = [
26, // namespace not found
60 // database not found
];
if (in_array($e->getCode(), $notFoundCodes, true)) {
return [];
}
throw $e;
}
return $cursor->toArray();
}
/**
* Counts records in specified collection.
* @param string $collectionName collection name
* @param array $condition filter condition
* @param array $options list of options in format: optionName => optionValue.
* @return int records count
*/
public function count($collectionName, $condition = [], $options = [])
{
$this->document = $this->db->getQueryBuilder()->count($collectionName, $condition, $options);
$result = current($this->execute()->toArray());
return $result['n'];
}
/**
* Adds the insert operation to the batch command.
* @param array $document document to be inserted
* @return $this self reference.
* @see executeBatch()
*/
public function addInsert($document)
{
$this->document[] = [
'type' => 'insert',
'document' => $document,
];
return $this;
}
/**
* Adds the update operation to the batch command.
* @param array $condition filter condition
* @param array $document data to be updated
* @param array $options update options.
* @return $this self reference.
* @see executeBatch()
*/
public function addUpdate($condition, $document, $options = [])
{
$options = array_merge(
[
'multi' => true,
'upsert' => false,
],
$options
);
if ($options['multi']) {
$keys = array_keys($document);
if (!empty($keys) && strncmp('$', $keys[0], 1) !== 0) {
$document = ['$set' => $document];
}
}
$this->document[] = [
'type' => 'update',
'condition' => $this->db->getQueryBuilder()->buildCondition($condition),
'document' => $document,
'options' => $options,
];
return $this;
}
/**
* Adds the delete operation to the batch command.
* @param array $condition filter condition.
* @param array $options delete options.
* @return $this self reference.
* @see executeBatch()
*/
public function addDelete($condition, $options = [])
{
$this->document[] = [
'type' => 'delete',
'condition' => $this->db->getQueryBuilder()->buildCondition($condition),
'options' => $options,
];
return $this;
}
/**
* Inserts new document into collection.
* @param string $collectionName collection name
* @param array $document document content
* @param array $options list of options in format: optionName => optionValue.
* @return ObjectID|bool inserted record ID, `false` - on failure.
*/
public function insert($collectionName, $document, $options = [])
{
$this->document = [];
$this->addInsert($document);
$result = $this->executeBatch($collectionName, $options);
if ($result['result']->getInsertedCount() < 1) {
return false;
}
return reset($result['insertedIds']);
}
/**
* Inserts batch of new documents into collection.
* @param string $collectionName collection name
* @param array[] $documents documents list
* @param array $options list of options in format: optionName => optionValue.
* @return array|false list of inserted IDs, `false` on failure.
*/
public function batchInsert($collectionName, $documents, $options = [])
{
$this->document = [];
foreach ($documents as $key => $document) {
$this->document[$key] = [
'type' => 'insert',
'document' => $document
];
}
$result = $this->executeBatch($collectionName, $options);
if ($result['result']->getInsertedCount() < 1) {
return false;
}
return $result['insertedIds'];
}
/**
* Update existing documents in the collection.
* @param string $collectionName collection name
* @param array $condition filter condition
* @param array $document data to be updated.
* @param array $options update options.
* @return WriteResult write result.
*/
public function update($collectionName, $condition, $document, $options = [])
{
$batchOptions = [];
foreach (['bypassDocumentValidation'] as $name) {
if (isset($options[$name])) {
$batchOptions[$name] = $options[$name];
unset($options[$name]);
}
}
$this->document = [];
$this->addUpdate($condition, $document, $options);
$result = $this->executeBatch($collectionName, $batchOptions);
return $result['result'];
}
/**
* Removes documents from the collection.
* @param string $collectionName collection name.
* @param array $condition filter condition.
* @param array $options delete options.
* @return WriteResult write result.
*/
public function delete($collectionName, $condition, $options = [])
{
$batchOptions = [];
foreach (['bypassDocumentValidation'] as $name) {
if (isset($options[$name])) {
$batchOptions[$name] = $options[$name];
unset($options[$name]);
}
}
$this->document = [];
$this->addDelete($condition, $options);
$result = $this->executeBatch($collectionName, $batchOptions);
return $result['result'];
}
/**
* Performs find query.
* @param string $collectionName collection name
* @param array $condition filter condition
* @param array $options query options.
* @return \MongoDB\Driver\Cursor result cursor.
*/
public function find($collectionName, $condition, $options = [])
{
$queryBuilder = $this->db->getQueryBuilder();
$this->document = $queryBuilder->buildCondition($condition);
if (isset($options['projection'])) {
$options['projection'] = $queryBuilder->buildSelectFields($options['projection']);
}
if (isset($options['sort'])) {
$options['sort'] = $queryBuilder->buildSortFields($options['sort']);
}
if (array_key_exists('limit', $options)) {
if ($options['limit'] === null || !ctype_digit((string) $options['limit'])) {
unset($options['limit']);
} else {
$options['limit'] = (int)$options['limit'];
}
}
if (array_key_exists('skip', $options)) {
if ($options['skip'] === null || !ctype_digit((string) $options['skip'])) {
unset($options['skip']);
} else {
$options['skip'] = (int)$options['skip'];
}
}
return $this->query($collectionName, $options);
}
/**
* Updates a document and returns it.
* @param $collectionName
* @param array $condition query condition
* @param array $update update criteria
* @param array $options list of options in format: optionName => optionValue.
* @return array|null the original document, or the modified document when $options['new'] is set.
*/
public function findAndModify($collectionName, $condition = [], $update = [], $options = [])
{
$this->document = $this->db->getQueryBuilder()->findAndModify($collectionName, $condition, $update, $options);
$cursor = $this->execute();
$result = current($cursor->toArray());
if (!isset($result['value'])) {
return null;
}
return $result['value'];
}
/**
* Returns a list of distinct values for the given column across a collection.
* @param string $collectionName collection name.
* @param string $fieldName field name to use.
* @param array $condition query parameters.
* @param array $options list of options in format: optionName => optionValue.
* @return array array of distinct values, or "false" on failure.
*/
public function distinct($collectionName, $fieldName, $condition = [], $options = [])
{
$this->document = $this->db->getQueryBuilder()->distinct($collectionName, $fieldName, $condition, $options);
$cursor = $this->execute();
$result = current($cursor->toArray());
if (!isset($result['values']) || !is_array($result['values'])) {
return false;
}
return $result['values'];
}
/**
* Performs aggregation using MongoDB "group" command.
* @param string $collectionName collection name.
* @param mixed $keys fields to group by. If an array or non-code object is passed,
* it will be the key used to group results. If instance of [[\MongoDB\BSON\Javascript]] passed,
* it will be treated as a function that returns the key to group by.
* @param array $initial Initial value of the aggregation counter object.
* @param \MongoDB\BSON\Javascript|string $reduce function that takes two arguments (the current
* document and the aggregation to this point) and does the aggregation.
* Argument will be automatically cast to [[\MongoDB\BSON\Javascript]].
* @param array $options optional parameters to the group command. Valid options include:
* - condition - criteria for including a document in the aggregation.
* - finalize - function called once per unique key that takes the final output of the reduce function.
* @return array the result of the aggregation.
*/
public function group($collectionName, $keys, $initial, $reduce, $options = [])
{
$this->document = $this->db->getQueryBuilder()->group($collectionName, $keys, $initial, $reduce, $options);
$cursor = $this->execute();
$result = current($cursor->toArray());
return $result['retval'];
}
/**
* Performs MongoDB "map-reduce" command.
* @param string $collectionName collection name.
* @param \MongoDB\BSON\Javascript|string $map function, which emits map data from collection.
* Argument will be automatically cast to [[\MongoDB\BSON\Javascript]].
* @param \MongoDB\BSON\Javascript|string $reduce function that takes two arguments (the map key
* and the map values) and does the aggregation.
* Argument will be automatically cast to [[\MongoDB\BSON\Javascript]].
* @param string|array $out output collection name. It could be a string for simple output
* ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']).
* You can pass ['inline' => true] to fetch the result at once without temporary collection usage.
* @param array $condition filter condition for including a document in the aggregation.
* @param array $options additional optional parameters to the mapReduce command. Valid options include:
*
* - sort: array, key to sort the input documents. The sort key must be in an existing index for this collection.
* - limit: int, the maximum number of documents to return in the collection.
* - finalize: \MongoDB\BSON\Javascript|string, function, which follows the reduce method and modifies the output.
* - scope: array, specifies global variables that are accessible in the map, reduce and finalize functions.
* - jsMode: bool, specifies whether to convert intermediate data into BSON format between the execution of the map and reduce functions.
* - verbose: bool, specifies whether to include the timing information in the result information.
*
* @return string|array the map reduce output collection name or output results.
*/
public function mapReduce($collectionName, $map, $reduce, $out, $condition = [], $options = [])
{
$this->document = $this->db->getQueryBuilder()->mapReduce($collectionName, $map, $reduce, $out, $condition, $options);
$cursor = $this->execute();
$result = current($cursor->toArray());
return array_key_exists('results', $result) ? $result['results'] : $result['result'];
}
/**
* Performs aggregation using MongoDB Aggregation Framework.
* In case 'cursor' option is specified [[\MongoDB\Driver\Cursor]] instance is returned,
* otherwise - an array of aggregation results.
* @param string $collectionName collection name
* @param array $pipelines list of pipeline operators.
* @param array $options optional parameters.
* @return array|\MongoDB\Driver\Cursor aggregation result.
*/
public function aggregate($collectionName, $pipelines, $options = [])
{
$this->document = $this->db->getQueryBuilder()->aggregate($collectionName, $pipelines, $options);
$cursor = $this->execute();
if (!empty($options['cursor'])) {
return $cursor;
}
$result = current($cursor->toArray());
return $result['result'];
}
/**
* Return an explanation of the query, often useful for optimization and debugging.
* @param string $collectionName collection name
* @param array $query query document.
* @return array explanation of the query.
*/
public function explain($collectionName, $query)
{
$this->document = $this->db->getQueryBuilder()->explain($collectionName, $query);
$cursor = $this->execute();
return current($cursor->toArray());
}
/**
* Returns the list of available databases.
* @param array $condition filter condition.
* @param array $options options list.
* @return array database information
*/
public function listDatabases($condition = [], $options = [])
{
if ($this->databaseName === null) {
$this->databaseName = 'admin';
}
$this->document = $this->db->getQueryBuilder()->listDatabases($condition, $options);
$cursor = $this->execute();
$result = current($cursor->toArray());
if (empty($result['databases'])) {
return [];
}
return $result['databases'];
}
/**
* Returns the list of available collections.
* @param array $condition filter condition.
* @param array $options options list.
* @return array collections information.
*/
public function listCollections($condition = [], $options = [])
{
$this->document = $this->db->getQueryBuilder()->listCollections($condition, $options);
$cursor = $this->execute();
return $cursor->toArray();
}
// Logging :
/**
* Logs the command data if logging is enabled at [[db]].
* @param array|string $namespace command namespace.
* @param array $data command data.
* @param string $category log category
* @return string|false log token, `false` if log is not enabled.
*/
protected function log($namespace, $data, $category)
{
if ($this->db->enableLogging) {
$token = $this->db->getLogBuilder()->generateToken($namespace, $data);
Yii::info($token, $category);
return $token;
}
return false;
}
/**
* Marks the beginning of a code block for profiling.
* @param string $token token for the code block
* @param string $category the category of this log message
* @see endProfile()
*/
protected function beginProfile($token, $category)
{
if ($token !== false && $this->db->enableProfiling) {
Yii::beginProfile($token, $category);
}
}
/**
* Marks the end of a code block for profiling.
* @param string $token token for the code block
* @param string $category the category of this log message
* @see beginProfile()
*/
protected function endProfile($token, $category)
{
if ($token !== false && $this->db->enableProfiling) {
Yii::endProfile($token, $category);
}
}
}