github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/aggregateoptions.go (about)

     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  import (
    10  	"time"
    11  
    12  	"go.mongodb.org/mongo-driver/bson"
    13  )
    14  
    15  // AggregateOptions represents options that can be used to configure an Aggregate operation.
    16  type AggregateOptions struct {
    17  	// If true, the operation can write to temporary files in the _tmp subdirectory of the database directory path on
    18  	// the server. The default value is false.
    19  	AllowDiskUse *bool
    20  
    21  	// The maximum number of documents to be included in each batch returned by the server.
    22  	BatchSize *int32
    23  
    24  	// If true, writes executed as part of the operation will opt out of document-level validation on the server. This
    25  	// option is valid for MongoDB versions >= 3.2 and is ignored for previous server versions. The default value is
    26  	// false. See https://www.mongodb.com/docs/manual/core/schema-validation/ for more information about document
    27  	// validation.
    28  	BypassDocumentValidation *bool
    29  
    30  	// Specifies a collation to use for string comparisons during the operation. This option is only valid for MongoDB
    31  	// versions >= 3.4. For previous server versions, the driver will return an error if this option is used. The
    32  	// default value is nil, which means the default collation of the collection will be used.
    33  	Collation *Collation
    34  
    35  	// The maximum amount of time that the query can run on the server. The default value is nil, meaning that there
    36  	// is no time limit for query execution.
    37  	//
    38  	// NOTE(benjirewis): MaxTime will be deprecated in a future release. The more general Timeout option may be used
    39  	// in its place to control the amount of time that a single operation can run before returning an error. MaxTime
    40  	// is ignored if Timeout is set on the client.
    41  	MaxTime *time.Duration
    42  
    43  	// The maximum amount of time that the server should wait for new documents to satisfy a tailable cursor query.
    44  	// This option is only valid for MongoDB versions >= 3.2 and is ignored for previous server versions.
    45  	MaxAwaitTime *time.Duration
    46  
    47  	// A string that will be included in server logs, profiling logs, and currentOp queries to help trace the operation.
    48  	// The default is nil, which means that no comment will be included in the logs.
    49  	Comment *string
    50  
    51  	// The index to use for the aggregation. This should either be the index name as a string or the index specification
    52  	// as a document. The hint does not apply to $lookup and $graphLookup aggregation stages. The driver will return an
    53  	// error if the hint parameter is a multi-key map. The default value is nil, which means that no hint will be sent.
    54  	Hint interface{}
    55  
    56  	// Specifies parameters for the aggregate expression. This option is only valid for MongoDB versions >= 5.0. Older
    57  	// servers will report an error for using this option. This must be a document mapping parameter names to values.
    58  	// Values must be constant or closed expressions that do not reference document fields. Parameters can then be
    59  	// accessed as variables in an aggregate expression context (e.g. "$$var").
    60  	Let interface{}
    61  
    62  	// Custom options to be added to aggregate expression. Key-value pairs of the BSON map should correlate with desired
    63  	// option names and values. Values must be Marshalable. Custom options may conflict with non-custom options, and custom
    64  	// options bypass client-side validation. Prefer using non-custom options where possible.
    65  	Custom bson.M
    66  }
    67  
    68  // Aggregate creates a new AggregateOptions instance.
    69  func Aggregate() *AggregateOptions {
    70  	return &AggregateOptions{}
    71  }
    72  
    73  // SetAllowDiskUse sets the value for the AllowDiskUse field.
    74  func (ao *AggregateOptions) SetAllowDiskUse(b bool) *AggregateOptions {
    75  	ao.AllowDiskUse = &b
    76  	return ao
    77  }
    78  
    79  // SetBatchSize sets the value for the BatchSize field.
    80  func (ao *AggregateOptions) SetBatchSize(i int32) *AggregateOptions {
    81  	ao.BatchSize = &i
    82  	return ao
    83  }
    84  
    85  // SetBypassDocumentValidation sets the value for the BypassDocumentValidation field.
    86  func (ao *AggregateOptions) SetBypassDocumentValidation(b bool) *AggregateOptions {
    87  	ao.BypassDocumentValidation = &b
    88  	return ao
    89  }
    90  
    91  // SetCollation sets the value for the Collation field.
    92  func (ao *AggregateOptions) SetCollation(c *Collation) *AggregateOptions {
    93  	ao.Collation = c
    94  	return ao
    95  }
    96  
    97  // SetMaxTime sets the value for the MaxTime field.
    98  //
    99  // NOTE(benjirewis): MaxTime will be deprecated in a future release. The more general Timeout
   100  // option may be used in its place to control the amount of time that a single operation can
   101  // run before returning an error. MaxTime is ignored if Timeout is set on the client.
   102  func (ao *AggregateOptions) SetMaxTime(d time.Duration) *AggregateOptions {
   103  	ao.MaxTime = &d
   104  	return ao
   105  }
   106  
   107  // SetMaxAwaitTime sets the value for the MaxAwaitTime field.
   108  func (ao *AggregateOptions) SetMaxAwaitTime(d time.Duration) *AggregateOptions {
   109  	ao.MaxAwaitTime = &d
   110  	return ao
   111  }
   112  
   113  // SetComment sets the value for the Comment field.
   114  func (ao *AggregateOptions) SetComment(s string) *AggregateOptions {
   115  	ao.Comment = &s
   116  	return ao
   117  }
   118  
   119  // SetHint sets the value for the Hint field.
   120  func (ao *AggregateOptions) SetHint(h interface{}) *AggregateOptions {
   121  	ao.Hint = h
   122  	return ao
   123  }
   124  
   125  // SetLet sets the value for the Let field.
   126  func (ao *AggregateOptions) SetLet(let interface{}) *AggregateOptions {
   127  	ao.Let = let
   128  	return ao
   129  }
   130  
   131  // SetCustom sets the value for the Custom field. Key-value pairs of the BSON map should correlate
   132  // with desired option names and values. Values must be Marshalable. Custom options may conflict
   133  // with non-custom options, and custom options bypass client-side validation. Prefer using non-custom
   134  // options where possible.
   135  func (ao *AggregateOptions) SetCustom(c bson.M) *AggregateOptions {
   136  	ao.Custom = c
   137  	return ao
   138  }
   139  
   140  // MergeAggregateOptions combines the given AggregateOptions instances into a single AggregateOptions in a last-one-wins
   141  // fashion.
   142  //
   143  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
   144  // single options struct instead.
   145  func MergeAggregateOptions(opts ...*AggregateOptions) *AggregateOptions {
   146  	aggOpts := Aggregate()
   147  	for _, ao := range opts {
   148  		if ao == nil {
   149  			continue
   150  		}
   151  		if ao.AllowDiskUse != nil {
   152  			aggOpts.AllowDiskUse = ao.AllowDiskUse
   153  		}
   154  		if ao.BatchSize != nil {
   155  			aggOpts.BatchSize = ao.BatchSize
   156  		}
   157  		if ao.BypassDocumentValidation != nil {
   158  			aggOpts.BypassDocumentValidation = ao.BypassDocumentValidation
   159  		}
   160  		if ao.Collation != nil {
   161  			aggOpts.Collation = ao.Collation
   162  		}
   163  		if ao.MaxTime != nil {
   164  			aggOpts.MaxTime = ao.MaxTime
   165  		}
   166  		if ao.MaxAwaitTime != nil {
   167  			aggOpts.MaxAwaitTime = ao.MaxAwaitTime
   168  		}
   169  		if ao.Comment != nil {
   170  			aggOpts.Comment = ao.Comment
   171  		}
   172  		if ao.Hint != nil {
   173  			aggOpts.Hint = ao.Hint
   174  		}
   175  		if ao.Let != nil {
   176  			aggOpts.Let = ao.Let
   177  		}
   178  		if ao.Custom != nil {
   179  			aggOpts.Custom = ao.Custom
   180  		}
   181  	}
   182  
   183  	return aggOpts
   184  }