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
7package options
8
9import "time"
10
11// CountOptions represents options that can be used to configure a CountDocuments operation.
12type CountOptions struct {
13	// Specifies a collation to use for string comparisons during the operation. This option is only valid for MongoDB
14	// versions >= 3.4. For previous server versions, the driver will return an error if this option is used. The
15	// default value is nil, which means the default collation of the collection will be used.
16	Collation *Collation
17
18	// The index to use for the aggregation. This should either be the index name as a string or the index specification
19	// as a document. The driver will return an error if the hint parameter is a multi-key map. The default value is nil,
20	// which means that no hint will be sent.
21	Hint interface{}
22
23	// The maximum number of documents to count. The default value is 0, which means that there is no limit and all
24	// documents matching the filter will be counted.
25	Limit *int64
26
27	// The maximum amount of time that the query can run on the server. The default value is nil, meaning that there is
28	// no time limit for query execution.
29	MaxTime *time.Duration
30
31	// The number of documents to skip before counting. The default value is 0.
32	Skip *int64
33}
34
35// Count creates a new CountOptions instance.
36func Count() *CountOptions {
37	return &CountOptions{}
38}
39
40// SetCollation sets the value for the Collation field.
41func (co *CountOptions) SetCollation(c *Collation) *CountOptions {
42	co.Collation = c
43	return co
44}
45
46// SetHint sets the value for the Hint field.
47func (co *CountOptions) SetHint(h interface{}) *CountOptions {
48	co.Hint = h
49	return co
50}
51
52// SetLimit sets the value for the Limit field.
53func (co *CountOptions) SetLimit(i int64) *CountOptions {
54	co.Limit = &i
55	return co
56}
57
58// SetMaxTime sets the value for the MaxTime field.
59func (co *CountOptions) SetMaxTime(d time.Duration) *CountOptions {
60	co.MaxTime = &d
61	return co
62}
63
64// SetSkip sets the value for the Skip field.
65func (co *CountOptions) SetSkip(i int64) *CountOptions {
66	co.Skip = &i
67	return co
68}
69
70// MergeCountOptions combines the given CountOptions instances into a single CountOptions in a last-one-wins fashion.
71func MergeCountOptions(opts ...*CountOptions) *CountOptions {
72	countOpts := Count()
73	for _, co := range opts {
74		if co == nil {
75			continue
76		}
77		if co.Collation != nil {
78			countOpts.Collation = co.Collation
79		}
80		if co.Hint != nil {
81			countOpts.Hint = co.Hint
82		}
83		if co.Limit != nil {
84			countOpts.Limit = co.Limit
85		}
86		if co.MaxTime != nil {
87			countOpts.MaxTime = co.MaxTime
88		}
89		if co.Skip != nil {
90			countOpts.Skip = co.Skip
91		}
92	}
93
94	return countOpts
95}
96