Skip to content

Commit 547db1d

Browse files
committed
add count
1 parent a9f34ad commit 547db1d

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

mongo/collection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,9 @@ func (coll *Collection) CountDocuments(ctx context.Context, filter interface{},
11271127
}
11281128
op.Hint(hintVal)
11291129
}
1130+
if args.RawData != nil {
1131+
op = op.RawData(*args.RawData)
1132+
}
11301133
retry := driver.RetryNone
11311134
if coll.client.retryReads {
11321135
retry = driver.RetryOncePerCommand
@@ -1208,6 +1211,9 @@ func (coll *Collection) EstimatedDocumentCount(
12081211
}
12091212
op = op.Comment(comment)
12101213
}
1214+
if args.RawData != nil {
1215+
op = op.RawData(*args.RawData)
1216+
}
12111217

12121218
retry := driver.RetryNone
12131219
if coll.client.retryReads {

mongo/options/countoptions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type CountOptions struct {
1616
Hint interface{}
1717
Limit *int64
1818
Skip *int64
19+
RawData *bool
1920
}
2021

2122
// CountOptionsBuilder contains options to configure count operations. Each
@@ -99,3 +100,15 @@ func (co *CountOptionsBuilder) SetSkip(i int64) *CountOptionsBuilder {
99100

100101
return co
101102
}
103+
104+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
105+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
106+
func (co *CountOptionsBuilder) SetRawData(rawData bool) *CountOptionsBuilder {
107+
co.Opts = append(co.Opts, func(opts *CountOptions) error {
108+
opts.RawData = &rawData
109+
110+
return nil
111+
})
112+
113+
return co
114+
}

mongo/options/estimatedcountoptions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package options
1212
// See corresponding setter methods for documentation.
1313
type EstimatedDocumentCountOptions struct {
1414
Comment interface{}
15+
RawData *bool
1516
}
1617

1718
// EstimatedDocumentCountOptionsBuilder contains options to estimate document
@@ -44,3 +45,15 @@ func (eco *EstimatedDocumentCountOptionsBuilder) SetComment(comment interface{})
4445

4546
return eco
4647
}
48+
49+
// SetRawData sets the value for the RawData field. If true, it allows the CRUD operations to access timeseries
50+
// collections on the bucket-level. This option is only valid for MongoDB versions >= 9.0. The default value is false.
51+
func (eco *EstimatedDocumentCountOptionsBuilder) SetRawData(rawData bool) *EstimatedDocumentCountOptionsBuilder {
52+
eco.Opts = append(eco.Opts, func(opts *EstimatedDocumentCountOptions) error {
53+
opts.RawData = &rawData
54+
55+
return nil
56+
})
57+
58+
return eco
59+
}

x/mongo/driver/operation/count.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Count struct {
4141
result CountResult
4242
serverAPI *driver.ServerAPIOptions
4343
timeout *time.Duration
44+
rawData *bool
4445
}
4546

4647
// CountResult represents a count result returned by the server.
@@ -139,14 +140,18 @@ func (c *Count) Execute(ctx context.Context) error {
139140
return err
140141
}
141142

142-
func (c *Count) command(dst []byte, _ description.SelectedServer) ([]byte, error) {
143+
func (c *Count) command(dst []byte, desc description.SelectedServer) ([]byte, error) {
143144
dst = bsoncore.AppendStringElement(dst, "count", c.collection)
144145
if c.query != nil {
145146
dst = bsoncore.AppendDocumentElement(dst, "query", c.query)
146147
}
147148
if c.comment.Type != bsoncore.Type(0) {
148149
dst = bsoncore.AppendValueElement(dst, "comment", c.comment)
149150
}
151+
// Set rawData for 8.2+ servers.
152+
if c.rawData != nil && desc.WireVersion != nil && driverutil.VersionRangeIncludes(*desc.WireVersion, 27) {
153+
dst = bsoncore.AppendBooleanElement(dst, "rawData", *c.rawData)
154+
}
150155
return dst, nil
151156
}
152157

@@ -310,3 +315,13 @@ func (c *Count) Authenticator(authenticator driver.Authenticator) *Count {
310315
c.authenticator = authenticator
311316
return c
312317
}
318+
319+
// RawData sets the rawData to access timeseries data in the compressed format.
320+
func (c *Count) RawData(rawData bool) *Count {
321+
if c == nil {
322+
c = new(Count)
323+
}
324+
325+
c.rawData = &rawData
326+
return c
327+
}

0 commit comments

Comments
 (0)