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 // UpdateOptions represents options that can be used to configure UpdateOne and UpdateMany operations. 10 type UpdateOptions struct { 11 // A set of filters specifying to which array elements an update should apply. This option is only valid for MongoDB 12 // versions >= 3.6. For previous server versions, the driver will return an error if this option is used. The 13 // default value is nil, which means the update will apply to all array elements. 14 ArrayFilters *ArrayFilters 15 16 // If true, writes executed as part of the operation will opt out of document-level validation on the server. This 17 // option is valid for MongoDB versions >= 3.2 and is ignored for previous server versions. The default value is 18 // false. See https://www.mongodb.com/docs/manual/core/schema-validation/ for more information about document 19 // validation. 20 BypassDocumentValidation *bool 21 22 // Specifies a collation to use for string comparisons during the operation. This option is only valid for MongoDB 23 // versions >= 3.4. For previous server versions, the driver will return an error if this option is used. The 24 // default value is nil, which means the default collation of the collection will be used. 25 Collation *Collation 26 27 // A string or document that will be included in server logs, profiling logs, and currentOp queries to help trace 28 // the operation. The default value is nil, which means that no comment will be included in the logs. 29 Comment interface{} 30 31 // The index to use for the operation. This should either be the index name as a string or the index specification 32 // as a document. This option is only valid for MongoDB versions >= 4.2. Server versions >= 3.4 will return an error 33 // if this option is specified. For server versions < 3.4, the driver will return a client-side error if this option 34 // is specified. The driver will return an error if this option is specified during an unacknowledged write 35 // operation. The driver will return an error if the hint parameter is a multi-key map. The default value is nil, 36 // which means that no hint will be sent. 37 Hint interface{} 38 39 // If true, a new document will be inserted if the filter does not match any documents in the collection. The 40 // default value is false. 41 Upsert *bool 42 43 // Specifies parameters for the update expression. This option is only valid for MongoDB versions >= 5.0. Older 44 // servers will report an error for using this option. This must be a document mapping parameter names to values. 45 // Values must be constant or closed expressions that do not reference document fields. Parameters can then be 46 // accessed as variables in an aggregate expression context (e.g. "$$var"). 47 Let interface{} 48 } 49 50 // Update creates a new UpdateOptions instance. 51 func Update() *UpdateOptions { 52 return &UpdateOptions{} 53 } 54 55 // SetArrayFilters sets the value for the ArrayFilters field. 56 func (uo *UpdateOptions) SetArrayFilters(af ArrayFilters) *UpdateOptions { 57 uo.ArrayFilters = &af 58 return uo 59 } 60 61 // SetBypassDocumentValidation sets the value for the BypassDocumentValidation field. 62 func (uo *UpdateOptions) SetBypassDocumentValidation(b bool) *UpdateOptions { 63 uo.BypassDocumentValidation = &b 64 return uo 65 } 66 67 // SetCollation sets the value for the Collation field. 68 func (uo *UpdateOptions) SetCollation(c *Collation) *UpdateOptions { 69 uo.Collation = c 70 return uo 71 } 72 73 // SetComment sets the value for the Comment field. 74 func (uo *UpdateOptions) SetComment(comment interface{}) *UpdateOptions { 75 uo.Comment = comment 76 return uo 77 } 78 79 // SetHint sets the value for the Hint field. 80 func (uo *UpdateOptions) SetHint(h interface{}) *UpdateOptions { 81 uo.Hint = h 82 return uo 83 } 84 85 // SetUpsert sets the value for the Upsert field. 86 func (uo *UpdateOptions) SetUpsert(b bool) *UpdateOptions { 87 uo.Upsert = &b 88 return uo 89 } 90 91 // SetLet sets the value for the Let field. 92 func (uo *UpdateOptions) SetLet(l interface{}) *UpdateOptions { 93 uo.Let = l 94 return uo 95 } 96 97 // MergeUpdateOptions combines the given UpdateOptions instances into a single UpdateOptions in a last-one-wins fashion. 98 // 99 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a 100 // single options struct instead. 101 func MergeUpdateOptions(opts ...*UpdateOptions) *UpdateOptions { 102 uOpts := Update() 103 for _, uo := range opts { 104 if uo == nil { 105 continue 106 } 107 if uo.ArrayFilters != nil { 108 uOpts.ArrayFilters = uo.ArrayFilters 109 } 110 if uo.BypassDocumentValidation != nil { 111 uOpts.BypassDocumentValidation = uo.BypassDocumentValidation 112 } 113 if uo.Collation != nil { 114 uOpts.Collation = uo.Collation 115 } 116 if uo.Comment != nil { 117 uOpts.Comment = uo.Comment 118 } 119 if uo.Hint != nil { 120 uOpts.Hint = uo.Hint 121 } 122 if uo.Upsert != nil { 123 uOpts.Upsert = uo.Upsert 124 } 125 if uo.Let != nil { 126 uOpts.Let = uo.Let 127 } 128 } 129 130 return uOpts 131 } 132