...

Source file src/go.mongodb.org/mongo-driver/mongo/options/replaceoptions.go

Documentation: go.mongodb.org/mongo-driver/mongo/options

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

View as plain text