2828use MongoDB \Builder \Type \QueryInterface ;
2929use MongoDB \Builder \Type \SearchOperatorInterface ;
3030use MongoDB \Driver \Cursor ;
31+ use MongoDB \Driver \ReadPreference ;
3132use Override ;
3233use RuntimeException ;
3334use stdClass ;
@@ -102,7 +103,7 @@ class Builder extends BaseBuilder
102103 /**
103104 * The maximum amount of seconds to allow the query to run.
104105 *
105- * @var int
106+ * @var int|float
106107 */
107108 public $ timeout ;
108109
@@ -113,6 +114,8 @@ class Builder extends BaseBuilder
113114 */
114115 public $ hint ;
115116
117+ private ReadPreference $ readPreference ;
118+
116119 /**
117120 * Custom options to add to the query.
118121 *
@@ -211,7 +214,7 @@ public function project($columns)
211214 /**
212215 * The maximum amount of seconds to allow the query to run.
213216 *
214- * @param int $seconds
217+ * @param int|float $seconds
215218 *
216219 * @return $this
217220 */
@@ -454,7 +457,7 @@ public function toMql(): array
454457
455458 // Apply order, offset, limit and projection
456459 if ($ this ->timeout ) {
457- $ options ['maxTimeMS ' ] = $ this ->timeout * 1000 ;
460+ $ options ['maxTimeMS ' ] = ( int ) ( $ this ->timeout * 1000 ) ;
458461 }
459462
460463 if ($ this ->orders ) {
@@ -1534,6 +1537,24 @@ public function options(array $options)
15341537 return $ this ;
15351538 }
15361539
1540+ /**
1541+ * Set the read preference for the query
1542+ *
1543+ * @see https://www.php.net/manual/en/class.mongodb-driver-readpreference.php
1544+ *
1545+ * @param string $mode
1546+ * @param array $tagSets
1547+ * @param array $options
1548+ *
1549+ * @return $this
1550+ */
1551+ public function readPreference (string $ mode , ?array $ tagSets = null , ?array $ options = null ): static
1552+ {
1553+ $ this ->readPreference = new ReadPreference ($ mode , $ tagSets , $ options );
1554+
1555+ return $ this ;
1556+ }
1557+
15371558 /**
15381559 * Performs a full-text search of the field or fields in an Atlas collection.
15391560 * NOTE: $search is only available for MongoDB Atlas clusters, and is not available for self-managed deployments.
@@ -1642,6 +1663,10 @@ private function inheritConnectionOptions(array $options = []): array
16421663 }
16431664 }
16441665
1666+ if (! isset ($ options ['readPreference ' ]) && isset ($ this ->readPreference )) {
1667+ $ options ['readPreference ' ] = $ this ->readPreference ;
1668+ }
1669+
16451670 return $ options ;
16461671 }
16471672
0 commit comments