You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

findoptions.go 24 kB

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