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.

replaceoptions.go 1.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // ReplaceOptions represents all possible options to the ReplaceOne() function.
  8. type ReplaceOptions struct {
  9. BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
  10. Collation *Collation // Specifies a collation
  11. Upsert *bool // When true, creates a new document if no document matches the query
  12. }
  13. // Replace returns a pointer to a new ReplaceOptions
  14. func Replace() *ReplaceOptions {
  15. return &ReplaceOptions{}
  16. }
  17. // SetBypassDocumentValidation allows the write to opt-out of document level validation.
  18. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  19. func (ro *ReplaceOptions) SetBypassDocumentValidation(b bool) *ReplaceOptions {
  20. ro.BypassDocumentValidation = &b
  21. return ro
  22. }
  23. // SetCollation specifies a collation.
  24. // Valid for servers >= 3.4
  25. func (ro *ReplaceOptions) SetCollation(c *Collation) *ReplaceOptions {
  26. ro.Collation = c
  27. return ro
  28. }
  29. // SetUpsert allows the creation of a new document if not document matches the query
  30. func (ro *ReplaceOptions) SetUpsert(b bool) *ReplaceOptions {
  31. ro.Upsert = &b
  32. return ro
  33. }
  34. // MergeReplaceOptions combines the argued ReplaceOptions into a single ReplaceOptions in a last-one-wins fashion
  35. func MergeReplaceOptions(opts ...*ReplaceOptions) *ReplaceOptions {
  36. rOpts := Replace()
  37. for _, ro := range opts {
  38. if ro == nil {
  39. continue
  40. }
  41. if ro.BypassDocumentValidation != nil {
  42. rOpts.BypassDocumentValidation = ro.BypassDocumentValidation
  43. }
  44. if ro.Collation != nil {
  45. rOpts.Collation = ro.Collation
  46. }
  47. if ro.Upsert != nil {
  48. rOpts.Upsert = ro.Upsert
  49. }
  50. }
  51. return rOpts
  52. }