Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package options |
| 8 | |
| 9 | import ( |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // FindOptions represent all possible options to the find() function. |
| 14 | type FindOptions struct { |
| 15 | AllowPartialResults *bool // If true, allows partial results to be returned if some shards are down. |
| 16 | BatchSize *int32 // Specifies the number of documents to return in every batch. |
| 17 | Collation *Collation // Specifies a collation to be used |
| 18 | Comment *string // Specifies a string to help trace the operation through the database. |
| 19 | CursorType *CursorType // Specifies the type of cursor to use |
| 20 | Hint interface{} // Specifies the index to use. |
| 21 | Limit *int64 // Sets a limit on the number of results to return. |
| 22 | Max interface{} // Sets an exclusive upper bound for a specific index |
| 23 | MaxAwaitTime *time.Duration // Specifies the maximum amount of time for the server to wait on new documents. |
| 24 | MaxTime *time.Duration // Specifies the maximum amount of time to allow the query to run. |
| 25 | Min interface{} // Specifies the inclusive lower bound for a specific index. |
| 26 | NoCursorTimeout *bool // If true, prevents cursors from timing out after an inactivity period. |
| 27 | OplogReplay *bool // Adds an option for internal use only and should not be set. |
| 28 | Projection interface{} // Limits the fields returned for all documents. |
| 29 | ReturnKey *bool // If true, only returns index keys for all result documents. |
| 30 | ShowRecordID *bool // If true, a $recordId field with the record identifier will be added to the returned documents. |
| 31 | Skip *int64 // Specifies the number of documents to skip before returning |
| 32 | Snapshot *bool // If true, prevents the cursor from returning a document more than once because of an intervening write operation. |
| 33 | Sort interface{} // Specifies the order in which to return results. |
| 34 | } |
| 35 | |
| 36 | // Find creates a new FindOptions instance. |
| 37 | func Find() *FindOptions { |
| 38 | return &FindOptions{} |
| 39 | } |
| 40 | |
| 41 | // SetAllowPartialResults sets whether partial results can be returned if some shards are down. |
| 42 | // For server versions < 3.2, this defaults to false. |
| 43 | func (f *FindOptions) SetAllowPartialResults(b bool) *FindOptions { |
| 44 | f.AllowPartialResults = &b |
| 45 | return f |
| 46 | } |
| 47 | |
| 48 | // SetBatchSize sets the number of documents to return in each batch. |
| 49 | func (f *FindOptions) SetBatchSize(i int32) *FindOptions { |
| 50 | f.BatchSize = &i |
| 51 | return f |
| 52 | } |
| 53 | |
| 54 | // SetCollation specifies a Collation to use for the Find operation. |
| 55 | // Valid for server versions >= 3.4 |
| 56 | func (f *FindOptions) SetCollation(collation *Collation) *FindOptions { |
| 57 | f.Collation = collation |
| 58 | return f |
| 59 | } |
| 60 | |
| 61 | // SetComment specifies a string to help trace the operation through the database. |
| 62 | func (f *FindOptions) SetComment(comment string) *FindOptions { |
| 63 | f.Comment = &comment |
| 64 | return f |
| 65 | } |
| 66 | |
| 67 | // SetCursorType specifes the type of cursor to use. |
| 68 | func (f *FindOptions) SetCursorType(ct CursorType) *FindOptions { |
| 69 | f.CursorType = &ct |
| 70 | return f |
| 71 | } |
| 72 | |
| 73 | // SetHint specifies the index to use. |
| 74 | func (f *FindOptions) SetHint(hint interface{}) *FindOptions { |
| 75 | f.Hint = hint |
| 76 | return f |
| 77 | } |
| 78 | |
| 79 | // SetLimit specifies a limit on the number of results. |
| 80 | // A negative limit implies that only 1 batch should be returned. |
| 81 | func (f *FindOptions) SetLimit(i int64) *FindOptions { |
| 82 | f.Limit = &i |
| 83 | return f |
| 84 | } |
| 85 | |
| 86 | // SetMax specifies an exclusive upper bound for a specific index. |
| 87 | func (f *FindOptions) SetMax(max interface{}) *FindOptions { |
| 88 | f.Max = max |
| 89 | return f |
| 90 | } |
| 91 | |
| 92 | // SetMaxAwaitTime specifies the max amount of time for the server to wait on new documents. |
| 93 | // If the cursor type is not TailableAwait, this option is ignored. |
| 94 | // For server versions < 3.2, this option is ignored. |
| 95 | func (f *FindOptions) SetMaxAwaitTime(d time.Duration) *FindOptions { |
| 96 | f.MaxAwaitTime = &d |
| 97 | return f |
| 98 | } |
| 99 | |
| 100 | // SetMaxTime specifies the max time to allow the query to run. |
| 101 | func (f *FindOptions) SetMaxTime(d time.Duration) *FindOptions { |
| 102 | f.MaxTime = &d |
| 103 | return f |
| 104 | } |
| 105 | |
| 106 | // SetMin specifies the inclusive lower bound for a specific index. |
| 107 | func (f *FindOptions) SetMin(min interface{}) *FindOptions { |
| 108 | f.Min = min |
| 109 | return f |
| 110 | } |
| 111 | |
| 112 | // SetNoCursorTimeout specifies whether or not cursors should time out after a period of inactivity. |
| 113 | // For server versions < 3.2, this defaults to false. |
| 114 | func (f *FindOptions) SetNoCursorTimeout(b bool) *FindOptions { |
| 115 | f.NoCursorTimeout = &b |
| 116 | return f |
| 117 | } |
| 118 | |
| 119 | // SetOplogReplay adds an option for internal use only and should not be set. |
| 120 | // For server versions < 3.2, this defaults to false. |
| 121 | func (f *FindOptions) SetOplogReplay(b bool) *FindOptions { |
| 122 | f.OplogReplay = &b |
| 123 | return f |
| 124 | } |
| 125 | |
| 126 | // SetProjection adds an option to limit the fields returned for all documents. |
| 127 | func (f *FindOptions) SetProjection(projection interface{}) *FindOptions { |
| 128 | f.Projection = projection |
| 129 | return f |
| 130 | } |
| 131 | |
| 132 | // SetReturnKey adds an option to only return index keys for all result documents. |
| 133 | func (f *FindOptions) SetReturnKey(b bool) *FindOptions { |
| 134 | f.ReturnKey = &b |
| 135 | return f |
| 136 | } |
| 137 | |
| 138 | // SetShowRecordID adds an option to determine whether to return the record identifier for each document. |
| 139 | // If true, a $recordId field will be added to each returned document. |
| 140 | func (f *FindOptions) SetShowRecordID(b bool) *FindOptions { |
| 141 | f.ShowRecordID = &b |
| 142 | return f |
| 143 | } |
| 144 | |
| 145 | // SetSkip specifies the number of documents to skip before returning. |
| 146 | // For server versions < 3.2, this defaults to 0. |
| 147 | func (f *FindOptions) SetSkip(i int64) *FindOptions { |
| 148 | f.Skip = &i |
| 149 | return f |
| 150 | } |
| 151 | |
| 152 | // SetSnapshot prevents the cursor from returning a document more than once because of an intervening write operation. |
| 153 | func (f *FindOptions) SetSnapshot(b bool) *FindOptions { |
| 154 | f.Snapshot = &b |
| 155 | return f |
| 156 | } |
| 157 | |
| 158 | // SetSort specifies the order in which to return documents. |
| 159 | func (f *FindOptions) SetSort(sort interface{}) *FindOptions { |
| 160 | f.Sort = sort |
| 161 | return f |
| 162 | } |
| 163 | |
| 164 | // MergeFindOptions combines the argued FindOptions into a single FindOptions in a last-one-wins fashion |
| 165 | func MergeFindOptions(opts ...*FindOptions) *FindOptions { |
| 166 | fo := Find() |
| 167 | for _, opt := range opts { |
| 168 | if opt == nil { |
| 169 | continue |
| 170 | } |
| 171 | if opt.AllowPartialResults != nil { |
| 172 | fo.AllowPartialResults = opt.AllowPartialResults |
| 173 | } |
| 174 | if opt.BatchSize != nil { |
| 175 | fo.BatchSize = opt.BatchSize |
| 176 | } |
| 177 | if opt.Collation != nil { |
| 178 | fo.Collation = opt.Collation |
| 179 | } |
| 180 | if opt.Comment != nil { |
| 181 | fo.Comment = opt.Comment |
| 182 | } |
| 183 | if opt.CursorType != nil { |
| 184 | fo.CursorType = opt.CursorType |
| 185 | } |
| 186 | if opt.Hint != nil { |
| 187 | fo.Hint = opt.Hint |
| 188 | } |
| 189 | if opt.Limit != nil { |
| 190 | fo.Limit = opt.Limit |
| 191 | } |
| 192 | if opt.Max != nil { |
| 193 | fo.Max = opt.Max |
| 194 | } |
| 195 | if opt.MaxAwaitTime != nil { |
| 196 | fo.MaxAwaitTime = opt.MaxAwaitTime |
| 197 | } |
| 198 | if opt.MaxTime != nil { |
| 199 | fo.MaxTime = opt.MaxTime |
| 200 | } |
| 201 | if opt.Min != nil { |
| 202 | fo.Min = opt.Min |
| 203 | } |
| 204 | if opt.NoCursorTimeout != nil { |
| 205 | fo.NoCursorTimeout = opt.NoCursorTimeout |
| 206 | } |
| 207 | if opt.OplogReplay != nil { |
| 208 | fo.OplogReplay = opt.OplogReplay |
| 209 | } |
| 210 | if opt.Projection != nil { |
| 211 | fo.Projection = opt.Projection |
| 212 | } |
| 213 | if opt.ReturnKey != nil { |
| 214 | fo.ReturnKey = opt.ReturnKey |
| 215 | } |
| 216 | if opt.ShowRecordID != nil { |
| 217 | fo.ShowRecordID = opt.ShowRecordID |
| 218 | } |
| 219 | if opt.Skip != nil { |
| 220 | fo.Skip = opt.Skip |
| 221 | } |
| 222 | if opt.Snapshot != nil { |
| 223 | fo.Snapshot = opt.Snapshot |
| 224 | } |
| 225 | if opt.Sort != nil { |
| 226 | fo.Sort = opt.Sort |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return fo |
| 231 | } |
| 232 | |
| 233 | // FindOneOptions represent all possible options to the findOne() function. |
| 234 | type FindOneOptions struct { |
| 235 | AllowPartialResults *bool // If true, allows partial results to be returned if some shards are down. |
| 236 | BatchSize *int32 // Specifies the number of documents to return in every batch. |
| 237 | Collation *Collation // Specifies a collation to be used |
| 238 | Comment *string // Specifies a string to help trace the operation through the database. |
| 239 | CursorType *CursorType // Specifies the type of cursor to use |
| 240 | Hint interface{} // Specifies the index to use. |
| 241 | Max interface{} // Sets an exclusive upper bound for a specific index |
| 242 | MaxAwaitTime *time.Duration // Specifies the maximum amount of time for the server to wait on new documents. |
| 243 | MaxTime *time.Duration // Specifies the maximum amount of time to allow the query to run. |
| 244 | Min interface{} // Specifies the inclusive lower bound for a specific index. |
| 245 | NoCursorTimeout *bool // If true, prevents cursors from timing out after an inactivity period. |
| 246 | OplogReplay *bool // Adds an option for internal use only and should not be set. |
| 247 | Projection interface{} // Limits the fields returned for all documents. |
| 248 | ReturnKey *bool // If true, only returns index keys for all result documents. |
| 249 | ShowRecordID *bool // If true, a $recordId field with the record identifier will be added to the returned documents. |
| 250 | Skip *int64 // Specifies the number of documents to skip before returning |
| 251 | Snapshot *bool // If true, prevents the cursor from returning a document more than once because of an intervening write operation. |
| 252 | Sort interface{} // Specifies the order in which to return results. |
| 253 | } |
| 254 | |
| 255 | // FindOne creates a new FindOneOptions instance. |
| 256 | func FindOne() *FindOneOptions { |
| 257 | return &FindOneOptions{} |
| 258 | } |
| 259 | |
| 260 | // SetAllowPartialResults sets whether partial results can be returned if some shards are down. |
| 261 | func (f *FindOneOptions) SetAllowPartialResults(b bool) *FindOneOptions { |
| 262 | f.AllowPartialResults = &b |
| 263 | return f |
| 264 | } |
| 265 | |
| 266 | // SetBatchSize sets the number of documents to return in each batch. |
| 267 | func (f *FindOneOptions) SetBatchSize(i int32) *FindOneOptions { |
| 268 | f.BatchSize = &i |
| 269 | return f |
| 270 | } |
| 271 | |
| 272 | // SetCollation specifies a Collation to use for the Find operation. |
| 273 | func (f *FindOneOptions) SetCollation(collation *Collation) *FindOneOptions { |
| 274 | f.Collation = collation |
| 275 | return f |
| 276 | } |
| 277 | |
| 278 | // SetComment specifies a string to help trace the operation through the database. |
| 279 | func (f *FindOneOptions) SetComment(comment string) *FindOneOptions { |
| 280 | f.Comment = &comment |
| 281 | return f |
| 282 | } |
| 283 | |
| 284 | // SetCursorType specifes the type of cursor to use. |
| 285 | func (f *FindOneOptions) SetCursorType(ct CursorType) *FindOneOptions { |
| 286 | f.CursorType = &ct |
| 287 | return f |
| 288 | } |
| 289 | |
| 290 | // SetHint specifies the index to use. |
| 291 | func (f *FindOneOptions) SetHint(hint interface{}) *FindOneOptions { |
| 292 | f.Hint = hint |
| 293 | return f |
| 294 | } |
| 295 | |
| 296 | // SetMax specifies an exclusive upper bound for a specific index. |
| 297 | func (f *FindOneOptions) SetMax(max interface{}) *FindOneOptions { |
| 298 | f.Max = max |
| 299 | return f |
| 300 | } |
| 301 | |
| 302 | // SetMaxAwaitTime specifies the max amount of time for the server to wait on new documents. |
| 303 | // For server versions < 3.2, this option is ignored. |
| 304 | func (f *FindOneOptions) SetMaxAwaitTime(d time.Duration) *FindOneOptions { |
| 305 | f.MaxAwaitTime = &d |
| 306 | return f |
| 307 | } |
| 308 | |
| 309 | // SetMaxTime specifies the max time to allow the query to run. |
| 310 | func (f *FindOneOptions) SetMaxTime(d time.Duration) *FindOneOptions { |
| 311 | f.MaxTime = &d |
| 312 | return f |
| 313 | } |
| 314 | |
| 315 | // SetMin specifies the inclusive lower bound for a specific index. |
| 316 | func (f *FindOneOptions) SetMin(min interface{}) *FindOneOptions { |
| 317 | f.Min = min |
| 318 | return f |
| 319 | } |
| 320 | |
| 321 | // SetNoCursorTimeout specifies whether or not cursors should time out after a period of inactivity. |
| 322 | func (f *FindOneOptions) SetNoCursorTimeout(b bool) *FindOneOptions { |
| 323 | f.NoCursorTimeout = &b |
| 324 | return f |
| 325 | } |
| 326 | |
| 327 | // SetOplogReplay adds an option for internal use only and should not be set. |
| 328 | func (f *FindOneOptions) SetOplogReplay(b bool) *FindOneOptions { |
| 329 | f.OplogReplay = &b |
| 330 | return f |
| 331 | } |
| 332 | |
| 333 | // SetProjection adds an option to limit the fields returned for all documents. |
| 334 | func (f *FindOneOptions) SetProjection(projection interface{}) *FindOneOptions { |
| 335 | f.Projection = projection |
| 336 | return f |
| 337 | } |
| 338 | |
| 339 | // SetReturnKey adds an option to only return index keys for all result documents. |
| 340 | func (f *FindOneOptions) SetReturnKey(b bool) *FindOneOptions { |
| 341 | f.ReturnKey = &b |
| 342 | return f |
| 343 | } |
| 344 | |
| 345 | // SetShowRecordID adds an option to determine whether to return the record identifier for each document. |
| 346 | // If true, a $recordId field will be added to each returned document. |
| 347 | func (f *FindOneOptions) SetShowRecordID(b bool) *FindOneOptions { |
| 348 | f.ShowRecordID = &b |
| 349 | return f |
| 350 | } |
| 351 | |
| 352 | // SetSkip specifies the number of documents to skip before returning. |
| 353 | func (f *FindOneOptions) SetSkip(i int64) *FindOneOptions { |
| 354 | f.Skip = &i |
| 355 | return f |
| 356 | } |
| 357 | |
| 358 | // SetSnapshot prevents the cursor from returning a document more than once because of an intervening write operation. |
| 359 | func (f *FindOneOptions) SetSnapshot(b bool) *FindOneOptions { |
| 360 | f.Snapshot = &b |
| 361 | return f |
| 362 | } |
| 363 | |
| 364 | // SetSort specifies the order in which to return documents. |
| 365 | func (f *FindOneOptions) SetSort(sort interface{}) *FindOneOptions { |
| 366 | f.Sort = sort |
| 367 | return f |
| 368 | } |
| 369 | |
| 370 | // MergeFindOneOptions combines the argued FindOneOptions into a single FindOneOptions in a last-one-wins fashion |
| 371 | func MergeFindOneOptions(opts ...*FindOneOptions) *FindOneOptions { |
| 372 | fo := FindOne() |
| 373 | for _, opt := range opts { |
| 374 | if opt == nil { |
| 375 | continue |
| 376 | } |
| 377 | if opt.AllowPartialResults != nil { |
| 378 | fo.AllowPartialResults = opt.AllowPartialResults |
| 379 | } |
| 380 | if opt.BatchSize != nil { |
| 381 | fo.BatchSize = opt.BatchSize |
| 382 | } |
| 383 | if opt.Collation != nil { |
| 384 | fo.Collation = opt.Collation |
| 385 | } |
| 386 | if opt.Comment != nil { |
| 387 | fo.Comment = opt.Comment |
| 388 | } |
| 389 | if opt.CursorType != nil { |
| 390 | fo.CursorType = opt.CursorType |
| 391 | } |
| 392 | if opt.Hint != nil { |
| 393 | fo.Hint = opt.Hint |
| 394 | } |
| 395 | if opt.Max != nil { |
| 396 | fo.Max = opt.Max |
| 397 | } |
| 398 | if opt.MaxAwaitTime != nil { |
| 399 | fo.MaxAwaitTime = opt.MaxAwaitTime |
| 400 | } |
| 401 | if opt.MaxTime != nil { |
| 402 | fo.MaxTime = opt.MaxTime |
| 403 | } |
| 404 | if opt.Min != nil { |
| 405 | fo.Min = opt.Min |
| 406 | } |
| 407 | if opt.NoCursorTimeout != nil { |
| 408 | fo.NoCursorTimeout = opt.NoCursorTimeout |
| 409 | } |
| 410 | if opt.OplogReplay != nil { |
| 411 | fo.OplogReplay = opt.OplogReplay |
| 412 | } |
| 413 | if opt.Projection != nil { |
| 414 | fo.Projection = opt.Projection |
| 415 | } |
| 416 | if opt.ReturnKey != nil { |
| 417 | fo.ReturnKey = opt.ReturnKey |
| 418 | } |
| 419 | if opt.ShowRecordID != nil { |
| 420 | fo.ShowRecordID = opt.ShowRecordID |
| 421 | } |
| 422 | if opt.Skip != nil { |
| 423 | fo.Skip = opt.Skip |
| 424 | } |
| 425 | if opt.Snapshot != nil { |
| 426 | fo.Snapshot = opt.Snapshot |
| 427 | } |
| 428 | if opt.Sort != nil { |
| 429 | fo.Sort = opt.Sort |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return fo |
| 434 | } |
| 435 | |
| 436 | // FindOneAndReplaceOptions represent all possible options to the findOne() function. |
| 437 | type FindOneAndReplaceOptions struct { |
| 438 | BypassDocumentValidation *bool // If true, allows the write to opt out of document-level validation. |
| 439 | Collation *Collation // Specifies a collation to be used |
| 440 | MaxTime *time.Duration // Specifies the maximum amount of time to allow the query to run. |
| 441 | Projection interface{} // Limits the fields returned for all documents. |
| 442 | ReturnDocument *ReturnDocument // Specifies whether the original or updated document should be returned. |
| 443 | Sort interface{} // Specifies the order in which to return results. |
| 444 | Upsert *bool // If true, creates a a new document if no document matches the query. |
| 445 | } |
| 446 | |
| 447 | // FindOneAndReplace creates a new FindOneAndReplaceOptions instance. |
| 448 | func FindOneAndReplace() *FindOneAndReplaceOptions { |
| 449 | return &FindOneAndReplaceOptions{} |
| 450 | } |
| 451 | |
| 452 | // SetBypassDocumentValidation specifies whether or not the write should opt out of document-level validation. |
| 453 | // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored. |
| 454 | func (f *FindOneAndReplaceOptions) SetBypassDocumentValidation(b bool) *FindOneAndReplaceOptions { |
| 455 | f.BypassDocumentValidation = &b |
| 456 | return f |
| 457 | } |
| 458 | |
| 459 | // SetCollation specifies a Collation to use for the Find operation. |
| 460 | func (f *FindOneAndReplaceOptions) SetCollation(collation *Collation) *FindOneAndReplaceOptions { |
| 461 | f.Collation = collation |
| 462 | return f |
| 463 | } |
| 464 | |
| 465 | // SetMaxTime specifies the max time to allow the query to run. |
| 466 | func (f *FindOneAndReplaceOptions) SetMaxTime(d time.Duration) *FindOneAndReplaceOptions { |
| 467 | f.MaxTime = &d |
| 468 | return f |
| 469 | } |
| 470 | |
| 471 | // SetProjection adds an option to limit the fields returned for all documents. |
| 472 | func (f *FindOneAndReplaceOptions) SetProjection(projection interface{}) *FindOneAndReplaceOptions { |
| 473 | f.Projection = projection |
| 474 | return f |
| 475 | } |
| 476 | |
| 477 | // SetReturnDocument specifies whether the original or updated document should be returned. |
| 478 | // If set to Before, the original document will be returned. If set to After, the updated document |
| 479 | // will be returned. |
| 480 | func (f *FindOneAndReplaceOptions) SetReturnDocument(rd ReturnDocument) *FindOneAndReplaceOptions { |
| 481 | f.ReturnDocument = &rd |
| 482 | return f |
| 483 | } |
| 484 | |
| 485 | // SetSort specifies the order in which to return documents. |
| 486 | func (f *FindOneAndReplaceOptions) SetSort(sort interface{}) *FindOneAndReplaceOptions { |
| 487 | f.Sort = sort |
| 488 | return f |
| 489 | } |
| 490 | |
| 491 | // SetUpsert specifies if a new document should be created if no document matches the query. |
| 492 | func (f *FindOneAndReplaceOptions) SetUpsert(b bool) *FindOneAndReplaceOptions { |
| 493 | f.Upsert = &b |
| 494 | return f |
| 495 | } |
| 496 | |
| 497 | // MergeFindOneAndReplaceOptions combines the argued FindOneAndReplaceOptions into a single FindOneAndReplaceOptions in a last-one-wins fashion |
| 498 | func MergeFindOneAndReplaceOptions(opts ...*FindOneAndReplaceOptions) *FindOneAndReplaceOptions { |
| 499 | fo := FindOneAndReplace() |
| 500 | for _, opt := range opts { |
| 501 | if opt == nil { |
| 502 | continue |
| 503 | } |
| 504 | if opt.BypassDocumentValidation != nil { |
| 505 | fo.BypassDocumentValidation = opt.BypassDocumentValidation |
| 506 | } |
| 507 | if opt.Collation != nil { |
| 508 | fo.Collation = opt.Collation |
| 509 | } |
| 510 | if opt.MaxTime != nil { |
| 511 | fo.MaxTime = opt.MaxTime |
| 512 | } |
| 513 | if opt.Projection != nil { |
| 514 | fo.Projection = opt.Projection |
| 515 | } |
| 516 | if opt.ReturnDocument != nil { |
| 517 | fo.ReturnDocument = opt.ReturnDocument |
| 518 | } |
| 519 | if opt.Sort != nil { |
| 520 | fo.Sort = opt.Sort |
| 521 | } |
| 522 | if opt.Upsert != nil { |
| 523 | fo.Upsert = opt.Upsert |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return fo |
| 528 | } |
| 529 | |
| 530 | // FindOneAndUpdateOptions represent all possible options to the findOne() function. |
| 531 | type FindOneAndUpdateOptions struct { |
| 532 | ArrayFilters *ArrayFilters // A set of filters specifying to which array elements an update should apply. |
| 533 | BypassDocumentValidation *bool // If true, allows the write to opt out of document-level validation. |
| 534 | Collation *Collation // Specifies a collation to be used |
| 535 | MaxTime *time.Duration // Specifies the maximum amount of time to allow the query to run. |
| 536 | Projection interface{} // Limits the fields returned for all documents. |
| 537 | ReturnDocument *ReturnDocument // Specifies whether the original or updated document should be returned. |
| 538 | Sort interface{} // Specifies the order in which to return results. |
| 539 | Upsert *bool // If true, creates a a new document if no document matches the query. |
| 540 | } |
| 541 | |
| 542 | // FindOneAndUpdate creates a new FindOneAndUpdateOptions instance. |
| 543 | func FindOneAndUpdate() *FindOneAndUpdateOptions { |
| 544 | return &FindOneAndUpdateOptions{} |
| 545 | } |
| 546 | |
| 547 | // SetBypassDocumentValidation sets filters that specify to which array elements an update should apply. |
| 548 | func (f *FindOneAndUpdateOptions) SetBypassDocumentValidation(b bool) *FindOneAndUpdateOptions { |
| 549 | f.BypassDocumentValidation = &b |
| 550 | return f |
| 551 | } |
| 552 | |
| 553 | // SetArrayFilters specifies a set of filters, which |
| 554 | func (f *FindOneAndUpdateOptions) SetArrayFilters(filters ArrayFilters) *FindOneAndUpdateOptions { |
| 555 | f.ArrayFilters = &filters |
| 556 | return f |
| 557 | } |
| 558 | |
| 559 | // SetCollation specifies a Collation to use for the Find operation. |
| 560 | func (f *FindOneAndUpdateOptions) SetCollation(collation *Collation) *FindOneAndUpdateOptions { |
| 561 | f.Collation = collation |
| 562 | return f |
| 563 | } |
| 564 | |
| 565 | // SetMaxTime specifies the max time to allow the query to run. |
| 566 | func (f *FindOneAndUpdateOptions) SetMaxTime(d time.Duration) *FindOneAndUpdateOptions { |
| 567 | f.MaxTime = &d |
| 568 | return f |
| 569 | } |
| 570 | |
| 571 | // SetProjection adds an option to limit the fields returned for all documents. |
| 572 | func (f *FindOneAndUpdateOptions) SetProjection(projection interface{}) *FindOneAndUpdateOptions { |
| 573 | f.Projection = projection |
| 574 | return f |
| 575 | } |
| 576 | |
| 577 | // SetReturnDocument specifies whether the original or updated document should be returned. |
| 578 | // If set to Before, the original document will be returned. If set to After, the updated document |
| 579 | // will be returned. |
| 580 | func (f *FindOneAndUpdateOptions) SetReturnDocument(rd ReturnDocument) *FindOneAndUpdateOptions { |
| 581 | f.ReturnDocument = &rd |
| 582 | return f |
| 583 | } |
| 584 | |
| 585 | // SetSort specifies the order in which to return documents. |
| 586 | func (f *FindOneAndUpdateOptions) SetSort(sort interface{}) *FindOneAndUpdateOptions { |
| 587 | f.Sort = sort |
| 588 | return f |
| 589 | } |
| 590 | |
| 591 | // SetUpsert specifies if a new document should be created if no document matches the query. |
| 592 | func (f *FindOneAndUpdateOptions) SetUpsert(b bool) *FindOneAndUpdateOptions { |
| 593 | f.Upsert = &b |
| 594 | return f |
| 595 | } |
| 596 | |
| 597 | // MergeFindOneAndUpdateOptions combines the argued FindOneAndUpdateOptions into a single FindOneAndUpdateOptions in a last-one-wins fashion |
| 598 | func MergeFindOneAndUpdateOptions(opts ...*FindOneAndUpdateOptions) *FindOneAndUpdateOptions { |
| 599 | fo := FindOneAndUpdate() |
| 600 | for _, opt := range opts { |
| 601 | if opt == nil { |
| 602 | continue |
| 603 | } |
| 604 | if opt.ArrayFilters != nil { |
| 605 | fo.ArrayFilters = opt.ArrayFilters |
| 606 | } |
| 607 | if opt.BypassDocumentValidation != nil { |
| 608 | fo.BypassDocumentValidation = opt.BypassDocumentValidation |
| 609 | } |
| 610 | if opt.Collation != nil { |
| 611 | fo.Collation = opt.Collation |
| 612 | } |
| 613 | if opt.MaxTime != nil { |
| 614 | fo.MaxTime = opt.MaxTime |
| 615 | } |
| 616 | if opt.Projection != nil { |
| 617 | fo.Projection = opt.Projection |
| 618 | } |
| 619 | if opt.ReturnDocument != nil { |
| 620 | fo.ReturnDocument = opt.ReturnDocument |
| 621 | } |
| 622 | if opt.Sort != nil { |
| 623 | fo.Sort = opt.Sort |
| 624 | } |
| 625 | if opt.Upsert != nil { |
| 626 | fo.Upsert = opt.Upsert |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return fo |
| 631 | } |
| 632 | |
| 633 | // FindOneAndDeleteOptions represent all possible options to the findOne() function. |
| 634 | type FindOneAndDeleteOptions struct { |
| 635 | Collation *Collation // Specifies a collation to be used |
| 636 | MaxTime *time.Duration // Specifies the maximum amount of time to allow the query to run. |
| 637 | Projection interface{} // Limits the fields returned for all documents. |
| 638 | Sort interface{} // Specifies the order in which to return results. |
| 639 | } |
| 640 | |
| 641 | // FindOneAndDelete creates a new FindOneAndDeleteOptions instance. |
| 642 | func FindOneAndDelete() *FindOneAndDeleteOptions { |
| 643 | return &FindOneAndDeleteOptions{} |
| 644 | } |
| 645 | |
| 646 | // SetCollation specifies a Collation to use for the Find operation. |
| 647 | // Valid for server versions >= 3.4 |
| 648 | func (f *FindOneAndDeleteOptions) SetCollation(collation *Collation) *FindOneAndDeleteOptions { |
| 649 | f.Collation = collation |
| 650 | return f |
| 651 | } |
| 652 | |
| 653 | // SetMaxTime specifies the max time to allow the query to run. |
| 654 | func (f *FindOneAndDeleteOptions) SetMaxTime(d time.Duration) *FindOneAndDeleteOptions { |
| 655 | f.MaxTime = &d |
| 656 | return f |
| 657 | } |
| 658 | |
| 659 | // SetProjection adds an option to limit the fields returned for all documents. |
| 660 | func (f *FindOneAndDeleteOptions) SetProjection(projection interface{}) *FindOneAndDeleteOptions { |
| 661 | f.Projection = projection |
| 662 | return f |
| 663 | } |
| 664 | |
| 665 | // SetSort specifies the order in which to return documents. |
| 666 | func (f *FindOneAndDeleteOptions) SetSort(sort interface{}) *FindOneAndDeleteOptions { |
| 667 | f.Sort = sort |
| 668 | return f |
| 669 | } |
| 670 | |
| 671 | // MergeFindOneAndDeleteOptions combines the argued FindOneAndDeleteOptions into a single FindOneAndDeleteOptions in a last-one-wins fashion |
| 672 | func MergeFindOneAndDeleteOptions(opts ...*FindOneAndDeleteOptions) *FindOneAndDeleteOptions { |
| 673 | fo := FindOneAndDelete() |
| 674 | for _, opt := range opts { |
| 675 | if opt == nil { |
| 676 | continue |
| 677 | } |
| 678 | if opt.Collation != nil { |
| 679 | fo.Collation = opt.Collation |
| 680 | } |
| 681 | if opt.MaxTime != nil { |
| 682 | fo.MaxTime = opt.MaxTime |
| 683 | } |
| 684 | if opt.Projection != nil { |
| 685 | fo.Projection = opt.Projection |
| 686 | } |
| 687 | if opt.Sort != nil { |
| 688 | fo.Sort = opt.Sort |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return fo |
| 693 | } |