28
28
use MongoDB \Builder \Type \QueryInterface ;
29
29
use MongoDB \Builder \Type \SearchOperatorInterface ;
30
30
use MongoDB \Driver \Cursor ;
31
+ use MongoDB \Driver \ReadPreference ;
31
32
use Override ;
32
33
use RuntimeException ;
33
34
use stdClass ;
@@ -102,7 +103,7 @@ class Builder extends BaseBuilder
102
103
/**
103
104
* The maximum amount of seconds to allow the query to run.
104
105
*
105
- * @var int
106
+ * @var int|float
106
107
*/
107
108
public $ timeout ;
108
109
@@ -113,6 +114,8 @@ class Builder extends BaseBuilder
113
114
*/
114
115
public $ hint ;
115
116
117
+ private ReadPreference $ readPreference ;
118
+
116
119
/**
117
120
* Custom options to add to the query.
118
121
*
@@ -211,7 +214,7 @@ public function project($columns)
211
214
/**
212
215
* The maximum amount of seconds to allow the query to run.
213
216
*
214
- * @param int $seconds
217
+ * @param int|float $seconds
215
218
*
216
219
* @return $this
217
220
*/
@@ -454,7 +457,7 @@ public function toMql(): array
454
457
455
458
// Apply order, offset, limit and projection
456
459
if ($ this ->timeout ) {
457
- $ options ['maxTimeMS ' ] = $ this ->timeout * 1000 ;
460
+ $ options ['maxTimeMS ' ] = ( int ) ( $ this ->timeout * 1000 ) ;
458
461
}
459
462
460
463
if ($ this ->orders ) {
@@ -1534,6 +1537,24 @@ public function options(array $options)
1534
1537
return $ this ;
1535
1538
}
1536
1539
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
+
1537
1558
/**
1538
1559
* Performs a full-text search of the field or fields in an Atlas collection.
1539
1560
* 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
1642
1663
}
1643
1664
}
1644
1665
1666
+ if (! isset ($ options ['readPreference ' ]) && isset ($ this ->readPreference )) {
1667
+ $ options ['readPreference ' ] = $ this ->readPreference ;
1668
+ }
1669
+
1645
1670
return $ options ;
1646
1671
}
1647
1672
0 commit comments