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.

updateoptions.go 2.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // UpdateOptions represents all possible options to the UpdateOne() and UpdateMany() functions.
  8. type UpdateOptions struct {
  9. ArrayFilters *ArrayFilters // A set of filters specifying to which array elements an update should apply
  10. BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
  11. Collation *Collation // Specifies a collation
  12. Upsert *bool // When true, creates a new document if no document matches the query
  13. }
  14. // Update returns a pointer to a new UpdateOptions
  15. func Update() *UpdateOptions {
  16. return &UpdateOptions{}
  17. }
  18. // SetArrayFilters specifies a set of filters specifying to which array elements an update should apply
  19. // Valid for server versions >= 3.6.
  20. func (uo *UpdateOptions) SetArrayFilters(af ArrayFilters) *UpdateOptions {
  21. uo.ArrayFilters = &af
  22. return uo
  23. }
  24. // SetBypassDocumentValidation allows the write to opt-out of document level validation.
  25. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  26. func (uo *UpdateOptions) SetBypassDocumentValidation(b bool) *UpdateOptions {
  27. uo.BypassDocumentValidation = &b
  28. return uo
  29. }
  30. // SetCollation specifies a collation.
  31. // Valid for server versions >= 3.4.
  32. func (uo *UpdateOptions) SetCollation(c *Collation) *UpdateOptions {
  33. uo.Collation = c
  34. return uo
  35. }
  36. // SetUpsert allows the creation of a new document if not document matches the query
  37. func (uo *UpdateOptions) SetUpsert(b bool) *UpdateOptions {
  38. uo.Upsert = &b
  39. return uo
  40. }
  41. // MergeUpdateOptions combines the argued UpdateOptions into a single UpdateOptions in a last-one-wins fashion
  42. func MergeUpdateOptions(opts ...*UpdateOptions) *UpdateOptions {
  43. uOpts := Update()
  44. for _, uo := range opts {
  45. if uo == nil {
  46. continue
  47. }
  48. if uo.ArrayFilters != nil {
  49. uOpts.ArrayFilters = uo.ArrayFilters
  50. }
  51. if uo.BypassDocumentValidation != nil {
  52. uOpts.BypassDocumentValidation = uo.BypassDocumentValidation
  53. }
  54. if uo.Collation != nil {
  55. uOpts.Collation = uo.Collation
  56. }
  57. if uo.Upsert != nil {
  58. uOpts.Upsert = uo.Upsert
  59. }
  60. }
  61. return uOpts
  62. }