Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Collection implements \Countable |
||
31 | { |
||
32 | /** |
||
33 | * @var string fully qualified class name of collection |
||
34 | */ |
||
35 | protected $mongoCollectionClassName = '\MongoCollection'; |
||
36 | |||
37 | /** |
||
38 | * @var string expression class. This class may be overloaded to define |
||
39 | * own query methods (whereUserAgeGreatedThan(), etc.) |
||
40 | * @deprecated since 1.13 Use 'expressionClass' declaration in mapping |
||
41 | */ |
||
42 | protected $_queryExpressionClass; |
||
43 | |||
44 | /** |
||
45 | * @deprecated since 1.13 Use 'documentClass' declaration in mapping |
||
46 | * @var string Default class for document |
||
47 | */ |
||
48 | private $documentClass; |
||
49 | |||
50 | /** |
||
51 | * List of arrays, where each item array is an index definition. |
||
52 | * Every index definition must contain key 'keys' with list of fields and orders, |
||
53 | * and optional options from @link http://php.net/manual/en/mongocollection.createindex.php: |
||
54 | * |
||
55 | * Example: |
||
56 | * array( |
||
57 | * array( |
||
58 | * 'keys' => array('field1' => 1, 'field2' => -1), |
||
59 | * 'unique' => true |
||
60 | * ), |
||
61 | * ... |
||
62 | * ) |
||
63 | * @var array list of indexes |
||
64 | * @deprecated since 1.13 Use 'index' declaration in mapping |
||
65 | */ |
||
66 | protected $_index; |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * @var \Sokil\Mongo\Database |
||
71 | */ |
||
72 | private $database; |
||
73 | |||
74 | /** |
||
75 | * |
||
76 | * @var \MongoCollection |
||
77 | */ |
||
78 | private $collection; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $collectionName; |
||
84 | |||
85 | /** |
||
86 | * Implementation of identity map pattern |
||
87 | * |
||
88 | * @var array list of cached documents |
||
89 | */ |
||
90 | private $documentPool = array(); |
||
91 | |||
92 | /** |
||
93 | * @deprecated since 1.13 Use 'versioning' declaration in mapping |
||
94 | * @var bool default value of versioning |
||
95 | */ |
||
96 | protected $versioning; |
||
97 | |||
98 | /** |
||
99 | * @var \Sokil\Mongo\Collection\Definition collection options |
||
100 | */ |
||
101 | private $definition; |
||
102 | |||
103 | public function __construct( |
||
104 | Database $database, |
||
105 | $collection, |
||
106 | Definition $definition = null |
||
107 | ) { |
||
108 | // define db |
||
109 | $this->database = $database; |
||
110 | |||
111 | // init mongo collection |
||
112 | if ($collection instanceof \MongoCollection) { |
||
113 | $this->collectionName = $collection->getName(); |
||
114 | $this->collection = $collection; |
||
115 | } else { |
||
116 | $this->collectionName = $collection; |
||
117 | } |
||
118 | |||
119 | // init definition |
||
120 | $this->definition = $definition ? $definition : new Definition(); |
||
121 | |||
122 | if (!empty($this->documentClass)) { |
||
|
|||
123 | $this->definition->setOption('documentClass', $this->documentClass); |
||
124 | } |
||
125 | |||
126 | if ($this->versioning !== null) { |
||
127 | $this->definition->setOption('versioning', $this->versioning); |
||
128 | } |
||
129 | |||
130 | if (!empty($this->_index)) { |
||
131 | $this->definition->setOption('index', $this->_index); |
||
132 | } |
||
133 | |||
134 | if (!empty($this->_queryExpressionClass)) { |
||
135 | $this->definition->setOption('expressionClass', $this->_queryExpressionClass); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Start versioning documents on modify |
||
141 | * |
||
142 | * @deprecated since 1.13 Use 'versioning' declaration in mapping |
||
143 | * @return \Sokil\Mongo\Collection |
||
144 | */ |
||
145 | public function enableVersioning() |
||
146 | { |
||
147 | $this->definition->setOption('versioning', true); |
||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if versioning enabled |
||
153 | * |
||
154 | * @deprecated since 1.13 Use 'versioning' declaration in mapping |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isVersioningEnabled() |
||
158 | { |
||
159 | return $this->definition->getOption('versioning'); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get option |
||
164 | * |
||
165 | * @param string|int $name |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getOption($name) |
||
169 | { |
||
170 | return $this->definition->getOption($name); |
||
171 | } |
||
172 | |||
173 | public function getOptions() |
||
174 | { |
||
175 | return $this->definition->getOptions(); |
||
176 | } |
||
177 | |||
178 | public function __get($name) |
||
179 | { |
||
180 | // support of deprecated property, use selg::getMongoCollection instead |
||
181 | if ($name === '_mongoCollection') { |
||
182 | return $this->getMongoCollection(); |
||
183 | } |
||
184 | |||
185 | return $this->getDocument($name); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get name of collection |
||
190 | * |
||
191 | * @return string name of collection |
||
192 | */ |
||
193 | public function getName() |
||
194 | { |
||
195 | return $this->collectionName; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get native collection instance of mongo driver |
||
200 | * |
||
201 | * @return \MongoCollection |
||
202 | */ |
||
203 | public function getMongoCollection() |
||
204 | { |
||
205 | if (empty($this->collection)) { |
||
206 | $mongoCollectionClassName = $this->mongoCollectionClassName; |
||
207 | $this->collection = new $mongoCollectionClassName( |
||
208 | $this->database->getMongoDB(), |
||
209 | $this->collectionName |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | return $this->collection; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * |
||
218 | * @return \Sokil\Mongo\Database |
||
219 | */ |
||
220 | public function getDatabase() |
||
221 | { |
||
222 | return $this->database; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Delete collection |
||
227 | * |
||
228 | * @return \Sokil\Mongo\Collection |
||
229 | * @throws \Sokil\Mongo\Exception |
||
230 | */ |
||
231 | public function delete() |
||
232 | { |
||
233 | $status = $this->getMongoCollection()->drop(); |
||
234 | if ($status['ok'] != 1) { |
||
235 | // check if collection exists |
||
236 | if ('ns not found' !== $status['errmsg']) { |
||
237 | // collection exist |
||
238 | throw new Exception('Error deleting collection ' . $this->getName() . ': ' . $status['errmsg']); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Override to define class name of document by document data |
||
247 | * |
||
248 | * @param array $documentData |
||
249 | * @return string Document class data |
||
250 | */ |
||
251 | public function getDocumentClassName(array $documentData = null) |
||
252 | { |
||
253 | $documentClass = $this->definition->getOption('documentClass'); |
||
254 | |||
255 | if (is_callable($documentClass)) { |
||
256 | return call_user_func($documentClass, $documentData); |
||
257 | } |
||
258 | |||
259 | if (class_exists($documentClass)) { |
||
260 | return $documentClass; |
||
261 | } |
||
262 | |||
263 | throw new Exception('Property "documentClass" must be callable or valid name of class'); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Factory method to get not stored Document instance from array |
||
268 | * @param array $data |
||
269 | * @return Document |
||
270 | */ |
||
271 | public function createDocument(array $data = null) |
||
272 | { |
||
273 | $className = $this->getDocumentClassName($data); |
||
274 | |||
275 | /* @var $document \Sokil\Mongo\Document */ |
||
276 | $document = new $className( |
||
277 | $this, |
||
278 | $data, |
||
279 | array('stored' => false) + $this->definition->getOptions() |
||
280 | ); |
||
281 | |||
282 | // store document to identity map |
||
283 | if ($this->isDocumentPoolEnabled()) { |
||
284 | $collection = $this; |
||
285 | $document->onAfterInsert(function (\Sokil\Mongo\Event $event) use ($collection) { |
||
286 | $collection->addDocumentToDocumentPool($event->getTarget()); |
||
287 | }); |
||
288 | } |
||
289 | |||
290 | return $document; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Factory method to get document object from array of stored document |
||
295 | * |
||
296 | * @param array $data |
||
297 | * @return \Sokil\Mongo\Document |
||
298 | */ |
||
299 | public function hydrate($data, $useDocumentPool = true) |
||
300 | { |
||
301 | if (!is_array($data) || !isset($data['_id'])) { |
||
302 | throw new Exception('Document must be stored and has _id key'); |
||
303 | } |
||
304 | |||
305 | // if document already in pool - return it |
||
306 | if ($useDocumentPool && $this->isDocumentPoolEnabled() && $this->isDocumentInDocumentPool($data['_id'])) { |
||
307 | return $this |
||
308 | ->getDocumentFromDocumentPool($data['_id']) |
||
309 | ->mergeUnmodified($data); |
||
310 | } |
||
311 | |||
312 | // init document instance |
||
313 | $className = $this->getDocumentClassName($data); |
||
314 | $document = new $className( |
||
315 | $this, |
||
316 | $data, |
||
317 | array('stored' => true) + $this->definition->getOptions() |
||
318 | ); |
||
319 | |||
320 | // store document in cache |
||
321 | if ($useDocumentPool && $this->isDocumentPoolEnabled()) { |
||
322 | $this->addDocumentToDocumentPool($document); |
||
323 | } |
||
324 | |||
325 | return $document; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Total count of documents in collection |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | public function count() |
||
334 | { |
||
335 | return $this->find()->count(); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Retrieve a list of distinct values for the given key across a collection. |
||
340 | * |
||
341 | * @param string $selector field selector |
||
342 | * @param array|callable|\Sokil\Mongo\Expression $expression expression to search documents |
||
343 | * @return array distinct values |
||
344 | */ |
||
345 | public function getDistinct($selector, $expression = null) |
||
346 | { |
||
347 | if ($expression) { |
||
348 | return $this->getMongoCollection()->distinct( |
||
349 | $selector, |
||
350 | Expression::convertToArray($expression) |
||
351 | ); |
||
352 | } |
||
353 | |||
354 | return $this->getMongoCollection()->distinct($selector); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Create new Expression instance to use in query builder or update operations |
||
359 | * |
||
360 | * @return \Sokil\Mongo\Expression |
||
361 | */ |
||
362 | public function expression() |
||
363 | { |
||
364 | $className = $this->definition->getExpressionClass(); |
||
365 | return new $className; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Create Operator instance to use in update operations |
||
370 | * |
||
371 | * @return \Sokil\Mongo\Operator |
||
372 | */ |
||
373 | public function operator() |
||
374 | { |
||
375 | return new Operator(); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Create document query builder |
||
380 | * |
||
381 | * @param $callable callable|null Function to configure query builder& |
||
382 | * @return Cursor |
||
383 | */ |
||
384 | public function find($callable = null) |
||
385 | { |
||
386 | /** @var Cursor $cursor */ |
||
387 | $cursor = new Cursor($this, array( |
||
388 | 'expressionClass' => $this->definition->getExpressionClass(), |
||
389 | 'batchSize' => $this->definition->getOption('batchSize'), |
||
390 | 'clientTimeout' => $this->definition->getOption('cursorClientTimeout'), |
||
391 | 'serverTimeout' => $this->definition->getOption('cursorServerTimeout'), |
||
392 | )); |
||
393 | |||
394 | if (is_callable($callable)) { |
||
395 | $callable($cursor->getExpression()); |
||
396 | } |
||
397 | |||
398 | return $cursor; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Create document query builder |
||
403 | * |
||
404 | * @param callable|null $callable |
||
405 | * |
||
406 | * @return \Sokil\Mongo\Cursor |
||
407 | */ |
||
408 | public function findAsArray($callable = null) |
||
414 | |||
415 | /** |
||
416 | * Stop storing found documents to pool |
||
417 | * |
||
418 | * @return \Sokil\Mongo\Collection |
||
419 | */ |
||
420 | public function disableDocumentPool() |
||
425 | |||
426 | /** |
||
427 | * Start storing found documents to pool |
||
428 | * |
||
429 | * @return \Sokil\Mongo\Collection |
||
430 | */ |
||
431 | public function enableDocumentPool() |
||
436 | |||
437 | /** |
||
438 | * Check if document pool enabled and requested documents store to it |
||
439 | * |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function isDocumentPoolEnabled() |
||
446 | |||
447 | public function clearDocumentPool() |
||
452 | |||
453 | /** |
||
454 | * Check if documents are in pool |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function isDocumentPoolEmpty() |
||
462 | |||
463 | /** |
||
464 | * Store document to pool |
||
465 | * |
||
466 | * @param array $document |
||
467 | * @return \Sokil\Mongo\Collection |
||
468 | */ |
||
469 | public function addDocumentToDocumentPool(Document $document) |
||
489 | |||
490 | /** |
||
491 | * Store documents to identity map |
||
492 | * |
||
493 | * @param array $documents list of Document instances |
||
494 | * @return \Sokil\Mongo\Collection |
||
495 | */ |
||
496 | public function addDocumentsToDocumentPool(array $documents) |
||
504 | |||
505 | /** |
||
506 | * Remove document instance from identity map |
||
507 | * |
||
508 | * @param \Sokil\Mongo\Document $document |
||
509 | * @return \Sokil\Mongo\Collection |
||
510 | */ |
||
511 | public function removeDocumentFromDocumentPool(Document $document) |
||
516 | |||
517 | /** |
||
518 | * Get document from identity map by it's id |
||
519 | * |
||
520 | * @param string|int|\MongoId $id |
||
521 | * @return \Sokil\Mongo\Document |
||
522 | */ |
||
523 | public function getDocumentFromDocumentPool($id) |
||
527 | |||
528 | /** |
||
529 | * Get documents from pool if they stored |
||
530 | * |
||
531 | * @param array $ids |
||
532 | */ |
||
533 | public function getDocumentsFromDocumentPool(array $ids = null) |
||
544 | |||
545 | /** |
||
546 | * Get number of documents in document pool |
||
547 | * |
||
548 | * @return int |
||
549 | */ |
||
550 | public function documentPoolCount() |
||
554 | |||
555 | /** |
||
556 | * Check if document exists in identity map |
||
557 | * |
||
558 | * @param \Sokil\Mongo\Document|\MongoId|int|string $document Document instance or it's id |
||
559 | * @return boolean |
||
560 | */ |
||
561 | public function isDocumentInDocumentPool($document) |
||
569 | |||
570 | /** |
||
571 | * Get document by id |
||
572 | * If callable specified, document always loaded directly omitting document pool. |
||
573 | * Method may return document as array if cursor configured through Cursor::asArray() |
||
574 | * |
||
575 | * @param string|\MongoId $id |
||
576 | * @param callable $callable cursor callable used to configure cursor |
||
577 | * @return \Sokil\Mongo\Document|array|null |
||
578 | */ |
||
579 | public function getDocument($id, $callable = null) |
||
599 | |||
600 | /** |
||
601 | * Get Document instance by it's reference |
||
602 | * |
||
603 | * @param array $ref reference to document |
||
604 | * @param bool $useDocumentPool try to get document from pool or fetch document from database |
||
605 | * |
||
606 | * @return Document|null |
||
607 | */ |
||
608 | View Code Duplication | public function getDocumentByReference(array $ref, $useDocumentPool = true) |
|
617 | |||
618 | /** |
||
619 | * Get document by id directly omitting cache |
||
620 | * Method may return document as array if cursor configured through Cursor::asArray() |
||
621 | * |
||
622 | * @param string|\MongoId $id |
||
623 | * @param callable $callable cursor callable used to configure cursor |
||
624 | * @return \Sokil\Mongo\Document|array|null |
||
625 | */ |
||
626 | public function getDocumentDirectly($id, $callable = null) |
||
639 | |||
640 | /** |
||
641 | * Check if document belongs to collection |
||
642 | * |
||
643 | * @param Document $document |
||
644 | * |
||
645 | * @return bool |
||
646 | */ |
||
647 | public function hasDocument(Document $document) |
||
648 | { |
||
649 | $documentCollection = $document->getCollection(); |
||
650 | $documentDatabase = $documentCollection->getDatabase(); |
||
651 | |||
652 | // check connection |
||
653 | if ($documentDatabase->getClient()->getDsn() !== $this->getDatabase()->getClient()->getDsn()) { |
||
654 | return false; |
||
655 | } |
||
656 | |||
657 | // check database |
||
665 | |||
666 | /** |
||
667 | * Get documents by list of id |
||
668 | * |
||
669 | * @param array $idList list of ids |
||
670 | * @param callable $callable cursor callable used to configure cursor |
||
671 | * |
||
672 | * @return Document[] |
||
673 | */ |
||
674 | public function getDocuments(array $idList, $callable = null) |
||
711 | |||
712 | /** |
||
713 | * Creates batch insert operation handler |
||
714 | * @param int|string $writeConcern Write concern. Default is 1 (Acknowledged) |
||
715 | * @param int $timeout Timeout for write concern. Default is 10000 milliseconds |
||
716 | * @param bool $ordered Determins if MongoDB must apply this batch in order (sequentally, |
||
717 | * one item at a time) or can rearrange it. Defaults to TRUE |
||
718 | * @return BatchInsert |
||
719 | */ |
||
720 | public function createBatchInsert($writeConcern = null, $timeout = null, $ordered = null) |
||
729 | |||
730 | /** |
||
731 | * Creates batch update operation handler |
||
732 | * @param int|string $writeConcern Write concern. Default is 1 (Acknowledged) |
||
733 | * @param int $timeout Timeout for write concern. Default is 10000 milliseconds |
||
734 | * @param bool $ordered Determins if MongoDB must apply this batch in order (sequentally, |
||
735 | * one item at a time) or can rearrange it. Defaults to TRUE |
||
736 | * @return BatchUpdate |
||
737 | */ |
||
738 | public function createBatchUpdate($writeConcern = null, $timeout = null, $ordered = null) |
||
747 | |||
748 | /** |
||
749 | * Creates batch delete operation handler |
||
750 | * @param int|string $writeConcern Write concern. Default is 1 (Acknowledged) |
||
751 | * @param int $timeout Timeout for write concern. Default is 10000 milliseconds |
||
752 | * @param bool $ordered Determins if MongoDB must apply this batch in order (sequentally, |
||
753 | * one item at a time) or can rearrange it. Defaults to TRUE |
||
754 | * @return BatchDelete |
||
755 | */ |
||
756 | public function createBatchDelete($writeConcern = null, $timeout = null, $ordered = null) |
||
765 | |||
766 | /** |
||
767 | * @deprecated since 1.13. Use Document::delete() |
||
768 | * @param \Sokil\Mongo\Document $document |
||
769 | * @return \Sokil\Mongo\Collection |
||
770 | */ |
||
771 | public function deleteDocument(Document $document) |
||
776 | |||
777 | /** |
||
778 | * Delete documents by expression |
||
779 | * |
||
780 | * @param Expression|callable|array $expression |
||
781 | * |
||
782 | * @return Collection |
||
783 | * |
||
784 | * @throws Exception |
||
785 | */ |
||
786 | public function batchDelete($expression) |
||
800 | |||
801 | /** |
||
802 | * @deprecated since 1.13. Use Collection::batchDelete(); |
||
803 | * |
||
804 | * @param Expression|callable|array $expression |
||
805 | * |
||
806 | * @return Collection |
||
807 | * |
||
808 | * @throws Exception |
||
809 | * |
||
810 | */ |
||
811 | public function deleteDocuments($expression = array()) |
||
815 | |||
816 | /** |
||
817 | * Insert multiple documents defined as arrays |
||
818 | * |
||
819 | * Prior to version 1.5.0 of the driver it was possible to use MongoCollection::batchInsert(), |
||
820 | * however, as of 1.5.0 that method is now discouraged. |
||
821 | * |
||
822 | * You can use Collection::createBatchInsert() |
||
823 | * |
||
824 | * @param array $rows list of documents to insert, defined as arrays |
||
825 | * @return \Sokil\Mongo\Collection |
||
826 | * @throws \Sokil\Mongo\Document\InvalidDocumentException |
||
827 | * @throws \Sokil\Mongo\Exception |
||
828 | */ |
||
829 | public function batchInsert($rows, $validate = true) |
||
865 | |||
866 | /** |
||
867 | * @deprecated since 1.13 Use Collection::batchInsert() |
||
868 | */ |
||
869 | public function insertMultiple($rows, $validate = true) |
||
873 | |||
874 | /** |
||
875 | * Direct insert of array to MongoDB without creating document object and validation |
||
876 | * |
||
877 | * @param array $document |
||
878 | * @return \Sokil\Mongo\Collection |
||
879 | * @throws Exception |
||
880 | */ |
||
881 | public function insert(array $document) |
||
901 | |||
902 | /** |
||
903 | * Update multiple documents |
||
904 | * |
||
905 | * @param \Sokil\Mongo\Expression|array|callable $expression expression to define |
||
906 | * which documents will change. |
||
907 | * @param \Sokil\Mongo\Operator|array|callable $updateData new data or operators to update |
||
908 | * @param array $options update options, see http://php.net/manual/ru/mongocollection.update.php |
||
909 | * @return \Sokil\Mongo\Collection |
||
910 | * @throws \Sokil\Mongo\Exception |
||
911 | */ |
||
912 | public function update($expression, $updateData, array $options = array()) |
||
936 | |||
937 | /** |
||
938 | * Update multiple documents |
||
939 | * |
||
940 | * @param \Sokil\Mongo\Expression|array|callable $expression expression to define |
||
941 | * which documents will change. |
||
942 | * @param \Sokil\Mongo\Operator|array|callable $updateData new data or operators |
||
943 | * to update |
||
944 | * @return \Sokil\Mongo\Collection |
||
945 | * @throws \Sokil\Mongo\Exception |
||
946 | */ |
||
947 | public function batchUpdate($expression, $updateData) |
||
953 | |||
954 | /** |
||
955 | * @deprecated since 1.13 Use Collection::batchUpdate() |
||
956 | */ |
||
957 | public function updateMultiple($expression, $updateData) |
||
961 | |||
962 | /** |
||
963 | * Update all documents |
||
964 | * |
||
965 | * @deprecated since 1.13. Use Collection::batchUpdate([]) |
||
966 | * @param \Sokil\Mongo\Operator|array|callable $updateData new data or operators |
||
967 | * @return \Sokil\Mongo\Collection |
||
968 | * @throws \Sokil\Mongo\Exception |
||
969 | */ |
||
970 | public function updateAll($updateData) |
||
976 | |||
977 | /** |
||
978 | * Start aggregation |
||
979 | * |
||
980 | * @link http://docs.mongodb.org/manual/reference/operator/aggregation/ |
||
981 | * @return \Sokil\Mongo\Pipeline |
||
982 | */ |
||
983 | public function createAggregator() |
||
987 | |||
988 | /** |
||
989 | * Aggregate using pipeline |
||
990 | * @link http://docs.mongodb.org/manual/reference/operator/aggregation/ |
||
991 | * |
||
992 | * @param callable|array|Pipeline $pipeline list of pipeline stages |
||
993 | * @param array aggregate options |
||
994 | * @param bool $asCursor return result as cursor |
||
995 | * |
||
996 | * @throws \Sokil\Mongo\Exception |
||
997 | * @return array result of aggregation |
||
998 | */ |
||
999 | public function aggregate( |
||
1062 | |||
1063 | /** |
||
1064 | * Check if aggragator options supported by database |
||
1065 | * |
||
1066 | * @param array $options |
||
1067 | * @throws FeatureNotSupportedException |
||
1068 | */ |
||
1069 | private function validateAggregationOptions(array $options) |
||
1111 | |||
1112 | /** |
||
1113 | * Explain aggregation |
||
1114 | * |
||
1115 | * @deprecated use pipeline option 'explain' in Collection::aggregate() or method Pipeline::explain() |
||
1116 | * @param array|Pipeline $pipeline |
||
1117 | * @return array result |
||
1118 | * @throws Exception |
||
1119 | */ |
||
1120 | public function explainAggregate($pipeline) |
||
1139 | |||
1140 | /** |
||
1141 | * Validates a collection. The method scans a collection’s data structures |
||
1142 | * for correctness and returns a single document that describes the |
||
1143 | * relationship between the logical collection and the physical |
||
1144 | * representation of the data. |
||
1145 | * |
||
1146 | * @link http://docs.mongodb.org/manual/reference/method/db.collection.validate/ |
||
1147 | * @param bool $full Specify true to enable a full validation and to return |
||
1148 | * full statistics. MongoDB disables full validation by default because it |
||
1149 | * is a potentially resource-intensive operation. |
||
1150 | * @return array |
||
1151 | * @throws Exception |
||
1152 | */ |
||
1153 | public function validate($full = false) |
||
1162 | |||
1163 | /** |
||
1164 | * Create index |
||
1165 | * |
||
1166 | * @deprecated since 1.19 Use self::createIndex() |
||
1167 | * @param array $key |
||
1168 | * @param array $options see @link http://php.net/manual/en/mongocollection.ensureindex.php |
||
1169 | * @return \Sokil\Mongo\Collection |
||
1170 | */ |
||
1171 | public function ensureIndex(array $key, array $options = array()) |
||
1175 | |||
1176 | /** |
||
1177 | * Create index |
||
1178 | * |
||
1179 | * @param array $key |
||
1180 | * @param array $options see @link http://php.net/manual/en/mongocollection.ensureindex.php |
||
1181 | * @return \Sokil\Mongo\Collection |
||
1182 | */ |
||
1183 | public function createIndex(array $key, array $options = array()) |
||
1188 | |||
1189 | /** |
||
1190 | * Delete index |
||
1191 | * |
||
1192 | * @param array $key |
||
1193 | * @return \Sokil\Mongo\Collection |
||
1194 | */ |
||
1195 | public function deleteIndex(array $key) |
||
1200 | |||
1201 | /** |
||
1202 | * Create unique index |
||
1203 | * |
||
1204 | * @param array $key |
||
1205 | * @param boolean $dropDups |
||
1206 | * @return \Sokil\Mongo\Collection |
||
1207 | */ |
||
1208 | public function ensureUniqueIndex(array $key, $dropDups = false) |
||
1217 | |||
1218 | /** |
||
1219 | * Create sparse index. |
||
1220 | * |
||
1221 | * Sparse indexes only contain entries for documents that have the indexed |
||
1222 | * field, even if the index field contains a null value. The index skips |
||
1223 | * over any document that is missing the indexed field. |
||
1224 | * |
||
1225 | * @link http://docs.mongodb.org/manual/core/index-sparse/ |
||
1226 | * |
||
1227 | * @param string|array $key An array specifying the index's fields as its |
||
1228 | * keys. For each field, the value is either the index direction or index |
||
1229 | * type. If specifying direction, specify 1 for ascending or -1 |
||
1230 | * for descending. |
||
1231 | * |
||
1232 | * @return Collection |
||
1233 | */ |
||
1234 | public function ensureSparseIndex(array $key) |
||
1245 | |||
1246 | /** |
||
1247 | * Create TTL index |
||
1248 | * |
||
1249 | * @link http://docs.mongodb.org/manual/tutorial/expire-data/ |
||
1250 | * |
||
1251 | * If seconds not specified then document expired at specified time, as described at |
||
1252 | * @link http://docs.mongodb.org/manual/tutorial/expire-data/#expire-documents-at-a-certain-clock-time |
||
1253 | * |
||
1254 | * @param string|array $key key must be date to use TTL |
||
1255 | * @param int $seconds |
||
1256 | * @return \Sokil\Mongo\Collection |
||
1257 | */ |
||
1258 | public function ensureTTLIndex(array $key, $seconds = 0) |
||
1266 | |||
1267 | /** |
||
1268 | * Create geo index 2dsphere |
||
1269 | * |
||
1270 | * @link http://docs.mongodb.org/manual/tutorial/build-a-2dsphere-index/ |
||
1271 | * |
||
1272 | * @param string $field |
||
1273 | * @return \Sokil\Mongo\Collection |
||
1274 | */ |
||
1275 | View Code Duplication | public function ensure2dSphereIndex($field) |
|
1289 | |||
1290 | /** |
||
1291 | * Create geo index 2dsphere |
||
1292 | * |
||
1293 | * @link http://docs.mongodb.org/manual/tutorial/build-a-2d-index/ |
||
1294 | * |
||
1295 | * @param string $field |
||
1296 | * @return \Sokil\Mongo\Collection |
||
1297 | */ |
||
1298 | View Code Duplication | public function ensure2dIndex($field) |
|
1312 | |||
1313 | /** |
||
1314 | * Create fulltext index |
||
1315 | * |
||
1316 | * @link https://docs.mongodb.org/manual/core/index-text/ |
||
1317 | * @link https://docs.mongodb.org/manual/tutorial/specify-language-for-text-index/ |
||
1318 | * |
||
1319 | * If a collection contains documents or embedded documents that are in different languages, |
||
1320 | * include a field named language in the documents or embedded documents and specify as its value the language |
||
1321 | * for that document or embedded document. |
||
1322 | * |
||
1323 | * The specified language in the document overrides the default language for the text index. |
||
1324 | * The specified language in an embedded document override the language specified in an enclosing document or |
||
1325 | * the default language for the index. |
||
1326 | * |
||
1327 | * @param array|string $field definition of fields where full text index ensured. May be |
||
1328 | * string to ensure index on one field, array of fields to |
||
1329 | * create full text index on few fields, and * widdcard '$**' to |
||
1330 | * create index on all fields of collection. Default value is '$**' |
||
1331 | * |
||
1332 | * @param array $weights For a text index, the weight of an indexed field denotes the |
||
1333 | * significance of the field relative to the other indexed fields |
||
1334 | * in terms of the text search score. |
||
1335 | * |
||
1336 | * @param string $defaultLanguage Default language associated with the indexed data determines |
||
1337 | * the rules to parse word roots (i.e. stemming) and ignore stop |
||
1338 | * words. The default language for the indexed data is english. |
||
1339 | * |
||
1340 | * @param string $languageOverride To use a field with a name other than language, include the |
||
1341 | * language_override option when creating the index. |
||
1342 | * |
||
1343 | * @return Collection |
||
1344 | */ |
||
1345 | public function ensureFulltextIndex( |
||
1378 | |||
1379 | |||
1380 | |||
1381 | /** |
||
1382 | * Create indexes based on self::$_index metadata |
||
1383 | * |
||
1384 | * @return \Sokil\Mongo\Collection |
||
1385 | * @throws \Exception |
||
1386 | */ |
||
1387 | public function initIndexes() |
||
1411 | |||
1412 | /** |
||
1413 | * Get index info |
||
1414 | * @return array |
||
1415 | */ |
||
1416 | public function getIndexes() |
||
1420 | |||
1421 | public function readPrimaryOnly() |
||
1426 | |||
1427 | public function readPrimaryPreferred(array $tags = null) |
||
1432 | |||
1433 | public function readSecondaryOnly(array $tags = null) |
||
1438 | |||
1439 | public function readSecondaryPreferred(array $tags = null) |
||
1444 | |||
1445 | public function readNearest(array $tags = null) |
||
1450 | |||
1451 | public function getReadPreference() |
||
1455 | |||
1456 | /** |
||
1457 | * Define write concern for all requests to current collection |
||
1458 | * |
||
1459 | * @param string|integer $w write concern |
||
1460 | * @param int $timeout timeout in milliseconds |
||
1461 | * @throws \Sokil\Mongo\Exception |
||
1462 | * @return \Sokil\Mongo\Collection |
||
1463 | */ |
||
1464 | public function setWriteConcern($w, $timeout = 10000) |
||
1472 | |||
1473 | /** |
||
1474 | * Define unacknowledged write concern for all requests to current collection |
||
1475 | * |
||
1476 | * @param int $timeout timeout in milliseconds |
||
1477 | * @throws \Sokil\Mongo\Exception |
||
1478 | * @return \Sokil\Mongo\Collection |
||
1479 | */ |
||
1480 | public function setUnacknowledgedWriteConcern($timeout = 10000) |
||
1485 | |||
1486 | /** |
||
1487 | * Define majority write concern for all requests to current collection |
||
1488 | * |
||
1489 | * @param int $timeout timeout in milliseconds |
||
1490 | * @throws \Sokil\Mongo\Exception |
||
1491 | * @return \Sokil\Mongo\Collection |
||
1492 | */ |
||
1493 | public function setMajorityWriteConcern($timeout = 10000) |
||
1498 | |||
1499 | /** |
||
1500 | * Get currently active write concern on all requests to collection |
||
1501 | * |
||
1502 | * @return int|string write concern |
||
1503 | */ |
||
1504 | public function getWriteConcern() |
||
1508 | |||
1509 | /** |
||
1510 | * Get collection stat |
||
1511 | * |
||
1512 | * @return array collection stat |
||
1513 | */ |
||
1514 | public function stats() |
||
1520 | } |
||
1521 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.