6
6
use MongoDB \Driver \Command ;
7
7
use MongoDB \Driver \Cursor ;
8
8
use MongoDB \Driver \Manager ;
9
- use MongoDB \Driver \Query ;
10
9
use MongoDB \Driver \ReadPreference ;
11
10
use MongoDB \Driver \Server ;
12
11
use MongoDB \Driver \WriteConcern ;
20
19
use MongoDB \Operation \Distinct ;
21
20
use MongoDB \Operation \DropCollection ;
22
21
use MongoDB \Operation \DropIndexes ;
22
+ use MongoDB \Operation \Find ;
23
+ use MongoDB \Operation \FindOne ;
23
24
use MongoDB \Operation \FindOneAndDelete ;
24
25
use MongoDB \Operation \FindOneAndReplace ;
25
26
use MongoDB \Operation \FindOneAndUpdate ;
29
30
class Collection
30
31
{
31
32
/* {{{ consts & vars */
32
- const QUERY_FLAG_TAILABLE_CURSOR = 0x02 ;
33
- const QUERY_FLAG_SLAVE_OKAY = 0x04 ;
34
- const QUERY_FLAG_OPLOG_REPLY = 0x08 ;
35
- const QUERY_FLAG_NO_CURSOR_TIMEOUT = 0x10 ;
36
- const QUERY_FLAG_AWAIT_DATA = 0x20 ;
37
- const QUERY_FLAG_EXHAUST = 0x40 ;
38
- const QUERY_FLAG_PARTIAL = 0x80 ;
39
-
40
-
41
- const CURSOR_TYPE_NON_TAILABLE = 0x00 ;
42
- const CURSOR_TYPE_TAILABLE = self ::QUERY_FLAG_TAILABLE_CURSOR ;
43
- //self::QUERY_FLAG_TAILABLE_CURSOR | self::QUERY_FLAG_AWAIT_DATA;
44
- const CURSOR_TYPE_TAILABLE_AWAIT = 0x22 ;
45
-
46
33
protected $ manager ;
47
34
protected $ ns ;
48
35
protected $ wc ;
@@ -396,50 +383,37 @@ public function dropIndexes()
396
383
}
397
384
398
385
/**
399
- * Performs a find (query) on the collection
386
+ * Finds documents matching the query.
400
387
*
388
+ * @see Find::__construct() for supported options
401
389
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
402
- * @see Collection::getFindOptions() for supported $options
403
- *
404
- * @param array $filter The find query to execute
405
- * @param array $options Additional options
390
+ * @param array $filter Query by which to filter documents
391
+ * @param array $options Additional options
406
392
* @return Cursor
407
393
*/
408
394
public function find (array $ filter = array (), array $ options = array ())
409
395
{
410
- $ options = array_merge ($ this ->getFindOptions (), $ options );
411
-
412
- $ query = $ this ->_buildQuery ($ filter , $ options );
413
-
414
- $ cursor = $ this ->manager ->executeQuery ($ this ->ns , $ query , $ this ->rp );
396
+ $ operation = new Find ($ this ->dbname , $ this ->collname , $ filter , $ options );
397
+ $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference::RP_PRIMARY ));
415
398
416
- return $ cursor ;
399
+ return $ operation -> execute ( $ server ) ;
417
400
}
418
401
419
402
/**
420
- * Performs a find (query) on the collection, returning at most one result
403
+ * Finds a single document matching the query.
421
404
*
405
+ * @see FindOne::__construct() for supported options
422
406
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
423
- * @see Collection::getFindOptions() for supported $options
424
- *
425
407
* @param array $filter The find query to execute
426
408
* @param array $options Additional options
427
- * @return array|false The matched document, or false on failure
409
+ * @return object|null
428
410
*/
429
411
public function findOne (array $ filter = array (), array $ options = array ())
430
412
{
431
- $ options = array_merge ($ this ->getFindOptions (), array ("limit " => 1 ), $ options );
432
-
433
- $ query = $ this ->_buildQuery ($ filter , $ options );
434
-
435
- $ cursor = $ this ->manager ->executeQuery ($ this ->ns , $ query , $ this ->rp );
436
-
437
- $ array = iterator_to_array ($ cursor );
438
- if ($ array ) {
439
- return $ array [0 ];
440
- }
413
+ $ operation = new FindOne ($ this ->dbname , $ this ->collname , $ filter , $ options );
414
+ $ server = $ this ->manager ->selectServer (new ReadPreference (ReadPreference::RP_PRIMARY ));
441
415
442
- return false ;
416
+ return $ operation -> execute ( $ server ) ;
443
417
}
444
418
445
419
/**
@@ -536,107 +510,6 @@ public function getDatabaseName()
536
510
return $ this ->dbname ;
537
511
}
538
512
539
- /**
540
- * Retrieves all find options with their default values.
541
- *
542
- * @return array of Collection::find() options
543
- */
544
- public function getFindOptions ()
545
- {
546
- return array (
547
- /**
548
- * Get partial results from a mongos if some shards are down (instead of throwing an error).
549
- *
550
- * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
551
- */
552
- "allowPartialResults " => false ,
553
-
554
- /**
555
- * The number of documents to return per batch.
556
- *
557
- * @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
558
- */
559
- "batchSize " => 101 ,
560
-
561
- /**
562
- * Attaches a comment to the query. If $comment also exists
563
- * in the modifiers document, the comment field overwrites $comment.
564
- *
565
- * @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
566
- */
567
- "comment " => "" ,
568
-
569
- /**
570
- * Indicates the type of cursor to use. This value includes both
571
- * the tailable and awaitData options.
572
- * The default is Collection::CURSOR_TYPE_NON_TAILABLE.
573
- *
574
- * @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
575
- */
576
- "cursorType " => self ::CURSOR_TYPE_NON_TAILABLE ,
577
-
578
- /**
579
- * The maximum number of documents to return.
580
- *
581
- * @see http://docs.mongodb.org/manual/reference/method/cursor.limit/
582
- */
583
- "limit " => 0 ,
584
-
585
- /**
586
- * The maximum amount of time to allow the query to run. If $maxTimeMS also exists
587
- * in the modifiers document, the maxTimeMS field overwrites $maxTimeMS.
588
- *
589
- * @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/
590
- */
591
- "maxTimeMS " => 0 ,
592
-
593
- /**
594
- * Meta-operators modifying the output or behavior of a query.
595
- *
596
- * @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
597
- */
598
- "modifiers " => array (),
599
-
600
- /**
601
- * The server normally times out idle cursors after an inactivity period (10 minutes)
602
- * to prevent excess memory use. Set this option to prevent that.
603
- *
604
- * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
605
- */
606
- "noCursorTimeout " => false ,
607
-
608
- /**
609
- * Internal replication use only - driver should not set
610
- *
611
- * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
612
- * @internal
613
- */
614
- "oplogReplay " => false ,
615
-
616
- /**
617
- * Limits the fields to return for all matching documents.
618
- *
619
- * @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
620
- */
621
- "projection " => array (),
622
-
623
- /**
624
- * The number of documents to skip before returning.
625
- *
626
- * @see http://docs.mongodb.org/manual/reference/method/cursor.skip/
627
- */
628
- "skip " => 0 ,
629
-
630
- /**
631
- * The order in which to return matching documents. If $orderby also exists
632
- * in the modifiers document, the sort field overwrites $orderby.
633
- *
634
- * @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
635
- */
636
- "sort " => array (),
637
- );
638
- }
639
-
640
513
/**
641
514
* Return the collection namespace.
642
515
*
@@ -793,35 +666,6 @@ public function updateOne(array $filter, array $update, array $options = array()
793
666
return new UpdateResult ($ wr );
794
667
}
795
668
796
- /**
797
- * Helper to build a Query object
798
- *
799
- * @param array $filter the query document
800
- * @param array $options query/protocol options
801
- * @return Query
802
- * @internal
803
- */
804
- final protected function _buildQuery ($ filter , $ options )
805
- {
806
- if ($ options ["comment " ]) {
807
- $ options ["modifiers " ]['$comment ' ] = $ options ["comment " ];
808
- }
809
- if ($ options ["maxTimeMS " ]) {
810
- $ options ["modifiers " ]['$maxTimeMS ' ] = $ options ["maxTimeMS " ];
811
- }
812
- if ($ options ["sort " ]) {
813
- $ options ['$orderby ' ] = $ options ["sort " ];
814
- }
815
-
816
- $ flags = $ this ->_opQueryFlags ($ options );
817
- $ options ["cursorFlags " ] = $ flags ;
818
-
819
-
820
- $ query = new Query ($ filter , $ options );
821
-
822
- return $ query ;
823
- }
824
-
825
669
/**
826
670
* Internal helper for delete one/many documents
827
671
* @internal
@@ -835,26 +679,6 @@ final protected function _delete($filter, $limit = 1)
835
679
return $ this ->manager ->executeBulkWrite ($ this ->ns , $ bulk , $ this ->wc );
836
680
}
837
681
838
- /**
839
- * Constructs the Query Wire Protocol field 'flags' based on $options
840
- * provided to other helpers
841
- *
842
- * @param array $options
843
- * @return integer OP_QUERY Wire Protocol flags
844
- * @internal
845
- */
846
- final protected function _opQueryFlags ($ options )
847
- {
848
- $ flags = 0 ;
849
-
850
- $ flags |= $ options ["allowPartialResults " ] ? self ::QUERY_FLAG_PARTIAL : 0 ;
851
- $ flags |= $ options ["cursorType " ] ? $ options ["cursorType " ] : 0 ;
852
- $ flags |= $ options ["oplogReplay " ] ? self ::QUERY_FLAG_OPLOG_REPLY : 0 ;
853
- $ flags |= $ options ["noCursorTimeout " ] ? self ::QUERY_FLAG_NO_CURSOR_TIMEOUT : 0 ;
854
-
855
- return $ flags ;
856
- }
857
-
858
682
/**
859
683
* Internal helper for replacing/updating one/many documents
860
684
* @internal
0 commit comments