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.

aggregateoptions.go 4.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import "time"
  8. // AggregateOptions represents all possible options to the Aggregate() function.
  9. type AggregateOptions struct {
  10. AllowDiskUse *bool // Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory
  11. BatchSize *int32 // The number of documents to return per batch
  12. BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation. This only applies when the $out stage is specified
  13. Collation *Collation // Specifies a collation
  14. MaxTime *time.Duration // The maximum amount of time to allow the query to run
  15. MaxAwaitTime *time.Duration // The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query
  16. Comment *string // Enables users to specify an arbitrary string to help trace the operation through the database profiler, currentOp and logs.
  17. Hint interface{} // The index to use for the aggregation. The hint does not apply to $lookup and $graphLookup stages
  18. }
  19. // Aggregate returns a pointer to a new AggregateOptions
  20. func Aggregate() *AggregateOptions {
  21. return &AggregateOptions{}
  22. }
  23. // SetAllowDiskUse enables writing to temporary files. When set to true,
  24. // aggregation stages can write data to the _tmp subdirectory in the
  25. // dbPath directory
  26. func (ao *AggregateOptions) SetAllowDiskUse(b bool) *AggregateOptions {
  27. ao.AllowDiskUse = &b
  28. return ao
  29. }
  30. // SetBatchSize specifies the number of documents to return per batch
  31. func (ao *AggregateOptions) SetBatchSize(i int32) *AggregateOptions {
  32. ao.BatchSize = &i
  33. return ao
  34. }
  35. // SetBypassDocumentValidation allows the write to opt-out of document level
  36. // validation. This only applies when the $out stage is specified
  37. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  38. func (ao *AggregateOptions) SetBypassDocumentValidation(b bool) *AggregateOptions {
  39. ao.BypassDocumentValidation = &b
  40. return ao
  41. }
  42. // SetCollation specifies a collation.
  43. // Valid for server versions >= 3.4
  44. func (ao *AggregateOptions) SetCollation(c *Collation) *AggregateOptions {
  45. ao.Collation = c
  46. return ao
  47. }
  48. // SetMaxTime specifies the maximum amount of time to allow the query to run
  49. func (ao *AggregateOptions) SetMaxTime(d time.Duration) *AggregateOptions {
  50. ao.MaxTime = &d
  51. return ao
  52. }
  53. // SetMaxAwaitTime specifies the maximum amount of time for the server to
  54. // wait on new documents to satisfy a tailable cursor query
  55. // For servers < 3.2, this option is ignored
  56. func (ao *AggregateOptions) SetMaxAwaitTime(d time.Duration) *AggregateOptions {
  57. ao.MaxAwaitTime = &d
  58. return ao
  59. }
  60. // SetComment enables users to specify an arbitrary string to help trace the
  61. // operation through the database profiler, currentOp and logs.
  62. func (ao *AggregateOptions) SetComment(s string) *AggregateOptions {
  63. ao.Comment = &s
  64. return ao
  65. }
  66. // SetHint specifies the index to use for the aggregation. The hint does not
  67. // apply to $lookup and $graphLookup stages
  68. func (ao *AggregateOptions) SetHint(h interface{}) *AggregateOptions {
  69. ao.Hint = h
  70. return ao
  71. }
  72. // MergeAggregateOptions combines the argued AggregateOptions into a single AggregateOptions in a last-one-wins fashion
  73. func MergeAggregateOptions(opts ...*AggregateOptions) *AggregateOptions {
  74. aggOpts := Aggregate()
  75. for _, ao := range opts {
  76. if ao == nil {
  77. continue
  78. }
  79. if ao.AllowDiskUse != nil {
  80. aggOpts.AllowDiskUse = ao.AllowDiskUse
  81. }
  82. if ao.BatchSize != nil {
  83. aggOpts.BatchSize = ao.BatchSize
  84. }
  85. if ao.BypassDocumentValidation != nil {
  86. aggOpts.BypassDocumentValidation = ao.BypassDocumentValidation
  87. }
  88. if ao.Collation != nil {
  89. aggOpts.Collation = ao.Collation
  90. }
  91. if ao.MaxTime != nil {
  92. aggOpts.MaxTime = ao.MaxTime
  93. }
  94. if ao.MaxAwaitTime != nil {
  95. aggOpts.MaxAwaitTime = ao.MaxAwaitTime
  96. }
  97. if ao.Comment != nil {
  98. aggOpts.Comment = ao.Comment
  99. }
  100. if ao.Hint != nil {
  101. aggOpts.Hint = ao.Hint
  102. }
  103. }
  104. return aggOpts
  105. }