33namespace MongoDB \Operation ;
44
55use MongoDB \Driver \Command ;
6+ use MongoDB \Driver \ReadConcern ;
67use MongoDB \Driver \ReadPreference ;
78use MongoDB \Driver \Server ;
89use MongoDB \Exception \InvalidArgumentException ;
1819 */
1920class Distinct implements Executable
2021{
22+ private static $ wireVersionForReadConcern = 4 ;
23+
2124 private $ databaseName ;
2225 private $ collectionName ;
2326 private $ fieldName ;
@@ -32,6 +35,11 @@ class Distinct implements Executable
3235 * * maxTimeMS (integer): The maximum amount of time to allow the query to
3336 * run.
3437 *
38+ * * readConcern (MongoDB\Driver\ReadConcern): Read concern.
39+ *
40+ * For servers < 3.2, this option is ignored as read concern is not
41+ * available.
42+ *
3543 * * readPreference (MongoDB\Driver\ReadPreference): Read preference.
3644 *
3745 * @param string $databaseName Database name
@@ -51,6 +59,10 @@ public function __construct($databaseName, $collectionName, $fieldName, $filter
5159 throw new InvalidArgumentTypeException ('"maxTimeMS" option ' , $ options ['maxTimeMS ' ], 'integer ' );
5260 }
5361
62+ if (isset ($ options ['readConcern ' ]) && ! $ options ['readConcern ' ] instanceof ReadConcern) {
63+ throw new InvalidArgumentTypeException ('"readConcern" option ' , $ options ['readConcern ' ], 'MongoDB\Driver\ReadConcern ' );
64+ }
65+
5466 if (isset ($ options ['readPreference ' ]) && ! $ options ['readPreference ' ] instanceof ReadPreference) {
5567 throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
5668 }
@@ -73,7 +85,7 @@ public function execute(Server $server)
7385 {
7486 $ readPreference = isset ($ this ->options ['readPreference ' ]) ? $ this ->options ['readPreference ' ] : null ;
7587
76- $ cursor = $ server ->executeCommand ($ this ->databaseName , $ this ->createCommand (), $ readPreference );
88+ $ cursor = $ server ->executeCommand ($ this ->databaseName , $ this ->createCommand ($ server ), $ readPreference );
7789 $ result = current ($ cursor ->toArray ());
7890
7991 if ( ! isset ($ result ->values ) || ! is_array ($ result ->values )) {
@@ -86,9 +98,10 @@ public function execute(Server $server)
8698 /**
8799 * Create the distinct command.
88100 *
101+ * @param Server $server
89102 * @return Command
90103 */
91- private function createCommand ()
104+ private function createCommand (Server $ server )
92105 {
93106 $ cmd = [
94107 'distinct ' => $ this ->collectionName ,
@@ -103,6 +116,10 @@ private function createCommand()
103116 $ cmd ['maxTimeMS ' ] = $ this ->options ['maxTimeMS ' ];
104117 }
105118
119+ if (isset ($ this ->options ['readConcern ' ]) && \MongoDB \server_supports_feature ($ server , self ::$ wireVersionForReadConcern )) {
120+ $ cmd ['readConcern ' ] = \MongoDB \read_concern_as_document ($ this ->options ['readConcern ' ]);
121+ }
122+
106123 return new Command ($ cmd );
107124 }
108125}
0 commit comments