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.

countoptions.go 2.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // CountOptions represents all possible options to the Count() function.
  9. type CountOptions struct {
  10. Collation *Collation // Specifies a collation
  11. Hint interface{} // The index to use
  12. Limit *int64 // The maximum number of documents to count
  13. MaxTime *time.Duration // The maximum amount of time to allow the operation to run
  14. Skip *int64 // The number of documents to skip before counting
  15. }
  16. // Count returns a pointer to a new CountOptions
  17. func Count() *CountOptions {
  18. return &CountOptions{}
  19. }
  20. // SetCollation specifies a collation
  21. // Valid for server versions >= 3.4
  22. func (co *CountOptions) SetCollation(c *Collation) *CountOptions {
  23. co.Collation = c
  24. return co
  25. }
  26. // SetHint specifies the index to use
  27. func (co *CountOptions) SetHint(h interface{}) *CountOptions {
  28. co.Hint = h
  29. return co
  30. }
  31. // SetLimit specifies the maximum number of documents to count
  32. func (co *CountOptions) SetLimit(i int64) *CountOptions {
  33. co.Limit = &i
  34. return co
  35. }
  36. // SetMaxTime specifies the maximum amount of time to allow the operation to run
  37. func (co *CountOptions) SetMaxTime(d time.Duration) *CountOptions {
  38. co.MaxTime = &d
  39. return co
  40. }
  41. // SetSkip specifies the number of documents to skip before counting
  42. func (co *CountOptions) SetSkip(i int64) *CountOptions {
  43. co.Skip = &i
  44. return co
  45. }
  46. // MergeCountOptions combines the argued CountOptions into a single CountOptions in a last-one-wins fashion
  47. func MergeCountOptions(opts ...*CountOptions) *CountOptions {
  48. countOpts := Count()
  49. for _, co := range opts {
  50. if co == nil {
  51. continue
  52. }
  53. if co.Collation != nil {
  54. countOpts.Collation = co.Collation
  55. }
  56. if co.Hint != nil {
  57. countOpts.Hint = co.Hint
  58. }
  59. if co.Limit != nil {
  60. countOpts.Limit = co.Limit
  61. }
  62. if co.MaxTime != nil {
  63. countOpts.MaxTime = co.MaxTime
  64. }
  65. if co.Skip != nil {
  66. countOpts.Skip = co.Skip
  67. }
  68. }
  69. return countOpts
  70. }