@@ -13,7 +13,8 @@ extension MongoCollection {
13
13
*/
14
14
public func find( _ filter: Document = [ : ] , options: FindOptions ? = nil ) throws -> MongoCursor < CollectionType > {
15
15
let opts = try BsonEncoder ( ) . encode ( options)
16
- guard let cursor = mongoc_collection_find_with_opts ( self . _collection, filter. data, opts? . data, nil ) else {
16
+ let rp = options? . readPreference? . _readPreference
17
+ guard let cursor = mongoc_collection_find_with_opts ( self . _collection, filter. data, opts? . data, rp) else {
17
18
throw MongoError . invalidResponse ( )
18
19
}
19
20
guard let client = self . _client else {
@@ -33,9 +34,10 @@ extension MongoCollection {
33
34
*/
34
35
public func aggregate( _ pipeline: [ Document ] , options: AggregateOptions ? = nil ) throws -> MongoCursor < Document > {
35
36
let opts = try BsonEncoder ( ) . encode ( options)
37
+ let rp = options? . readPreference? . _readPreference
36
38
let pipeline : Document = [ " pipeline " : pipeline]
37
39
guard let cursor = mongoc_collection_aggregate (
38
- self . _collection, MONGOC_QUERY_NONE, pipeline. data, opts? . data, nil ) else {
40
+ self . _collection, MONGOC_QUERY_NONE, pipeline. data, opts? . data, rp ) else {
39
41
throw MongoError . invalidResponse ( )
40
42
}
41
43
guard let client = self . _client else {
@@ -55,11 +57,12 @@ extension MongoCollection {
55
57
*/
56
58
public func count( _ filter: Document = [ : ] , options: CountOptions ? = nil ) throws -> Int {
57
59
let opts = try BsonEncoder ( ) . encode ( options)
60
+ let rp = options? . readPreference? . _readPreference
58
61
var error = bson_error_t ( )
59
62
// because we already encode skip and limit in the options,
60
63
// pass in 0s so we don't get duplicate parameter errors.
61
64
let count = mongoc_collection_count_with_opts (
62
- self . _collection, MONGOC_QUERY_NONE, filter. data, 0 , 0 , opts? . data, nil , & error)
65
+ self . _collection, MONGOC_QUERY_NONE, filter. data, 0 , 0 , opts? . data, rp , & error)
63
66
64
67
if count == - 1 { throw MongoError . commandError ( message: toErrorString ( error) ) }
65
68
@@ -90,10 +93,11 @@ extension MongoCollection {
90
93
]
91
94
92
95
let opts = try BsonEncoder ( ) . encode ( options)
96
+ let rp = options? . readPreference? . _readPreference
93
97
let reply = Document ( )
94
98
var error = bson_error_t ( )
95
99
if !mongoc_collection_read_command_with_opts(
96
- self . _collection, command. data, nil , opts? . data, reply. data, & error) {
100
+ self . _collection, command. data, rp , opts? . data, reply. data, & error) {
97
101
throw MongoError . commandError ( message: toErrorString ( error) )
98
102
}
99
103
@@ -164,13 +168,17 @@ public struct AggregateOptions: Encodable {
164
168
/// A `ReadConcern` to use in read stages of this operation.
165
169
public let readConcern : ReadConcern ?
166
170
171
+ /// A ReadPreference to use for this operation.
172
+ public let readPreference : ReadPreference ?
173
+
167
174
/// A `WriteConcern` to use in `$out` stages of this operation.
168
175
public let writeConcern : WriteConcern ?
169
176
170
177
/// Convenience initializer allowing any/all parameters to be optional
171
178
public init ( allowDiskUse: Bool ? = nil , batchSize: Int32 ? = nil , bypassDocumentValidation: Bool ? = nil ,
172
179
collation: Document ? = nil , comment: String ? = nil , hint: Hint ? = nil , maxTimeMS: Int64 ? = nil ,
173
- readConcern: ReadConcern ? = nil , writeConcern: WriteConcern ? = nil ) {
180
+ readConcern: ReadConcern ? = nil , readPreference: ReadPreference ? = nil ,
181
+ writeConcern: WriteConcern ? = nil ) {
174
182
self . allowDiskUse = allowDiskUse
175
183
self . batchSize = batchSize
176
184
self . bypassDocumentValidation = bypassDocumentValidation
@@ -179,8 +187,14 @@ public struct AggregateOptions: Encodable {
179
187
self . hint = hint
180
188
self . maxTimeMS = maxTimeMS
181
189
self . readConcern = readConcern
190
+ self . readPreference = readPreference
182
191
self . writeConcern = writeConcern
183
192
}
193
+
194
+ private enum CodingKeys : String , CodingKey {
195
+ case allowDiskUse, batchSize, bypassDocumentValidation, collation, maxTimeMS, comment, hint, readConcern,
196
+ writeConcern
197
+ }
184
198
}
185
199
186
200
/// Options to use when executing a `count` command on a `MongoCollection`.
@@ -203,16 +217,24 @@ public struct CountOptions: Encodable {
203
217
/// A ReadConcern to use for this operation.
204
218
public let readConcern : ReadConcern ?
205
219
220
+ /// A ReadPreference to use for this operation.
221
+ public let readPreference : ReadPreference ?
222
+
206
223
/// Convenience initializer allowing any/all parameters to be optional
207
224
public init ( collation: Document ? = nil , hint: Hint ? = nil , limit: Int64 ? = nil , maxTimeMS: Int64 ? = nil ,
208
- readConcern: ReadConcern ? = nil , skip: Int64 ? = nil ) {
225
+ readConcern: ReadConcern ? = nil , readPreference : ReadPreference ? = nil , skip: Int64 ? = nil ) {
209
226
self . collation = collation
210
227
self . hint = hint
211
228
self . limit = limit
212
229
self . maxTimeMS = maxTimeMS
213
230
self . readConcern = readConcern
231
+ self . readPreference = readPreference
214
232
self . skip = skip
215
233
}
234
+
235
+ private enum CodingKeys : String , CodingKey {
236
+ case collation, hint, limit, maxTimeMS, readConcern, skip
237
+ }
216
238
}
217
239
218
240
/// Options to use when executing a `distinct` command on a `MongoCollection`.
@@ -226,11 +248,20 @@ public struct DistinctOptions: Encodable {
226
248
/// A ReadConcern to use for this operation.
227
249
public let readConcern : ReadConcern ?
228
250
251
+ /// A ReadPreference to use for this operation.
252
+ public let readPreference : ReadPreference ?
253
+
229
254
/// Convenience initializer allowing any/all parameters to be optional
230
- public init ( collation: Document ? = nil , maxTimeMS: Int64 ? = nil , readConcern: ReadConcern ? = nil ) {
255
+ public init ( collation: Document ? = nil , maxTimeMS: Int64 ? = nil , readConcern: ReadConcern ? = nil ,
256
+ readPreference: ReadPreference ? = nil ) {
231
257
self . collation = collation
232
258
self . maxTimeMS = maxTimeMS
233
259
self . readConcern = readConcern
260
+ self . readPreference = readPreference
261
+ }
262
+
263
+ private enum CodingKeys : String , CodingKey {
264
+ case collation, maxTimeMS, readConcern
234
265
}
235
266
}
236
267
@@ -333,13 +364,16 @@ public struct FindOptions: Encodable {
333
364
/// A ReadConcern to use for this operation.
334
365
public let readConcern : ReadConcern ?
335
366
367
+ /// A ReadPreference to use for this operation.
368
+ public let readPreference : ReadPreference ?
369
+
336
370
/// Convenience initializer allowing any/all parameters to be optional
337
371
public init ( allowPartialResults: Bool ? = nil , batchSize: Int32 ? = nil , collation: Document ? = nil ,
338
372
comment: String ? = nil , cursorType: CursorType ? = nil , hint: Hint ? = nil , limit: Int64 ? = nil ,
339
373
max: Document ? = nil , maxAwaitTimeMS: Int64 ? = nil , maxScan: Int64 ? = nil , maxTimeMS: Int64 ? = nil ,
340
374
min: Document ? = nil , noCursorTimeout: Bool ? = nil , projection: Document ? = nil ,
341
- readConcern: ReadConcern ? = nil , returnKey : Bool ? = nil , showRecordId : Bool ? = nil , skip : Int64 ? = nil ,
342
- sort: Document ? = nil ) {
375
+ readConcern: ReadConcern ? = nil , readPreference : ReadPreference ? = nil , returnKey : Bool ? = nil ,
376
+ showRecordId : Bool ? = nil , skip : Int64 ? = nil , sort: Document ? = nil ) {
343
377
self . allowPartialResults = allowPartialResults
344
378
self . batchSize = batchSize
345
379
self . collation = collation
@@ -358,6 +392,7 @@ public struct FindOptions: Encodable {
358
392
self . noCursorTimeout = noCursorTimeout
359
393
self . projection = projection
360
394
self . readConcern = readConcern
395
+ self . readPreference = readPreference
361
396
self . returnKey = returnKey
362
397
self . showRecordId = showRecordId
363
398
self . skip = skip
0 commit comments