Skip to content

Commit 48027d6

Browse files
authored
GODRIVER-2435 Add comment field to distinct helper. (#993)
1 parent 27a230b commit 48027d6

File tree

6 files changed

+313
-0
lines changed

6 files changed

+313
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"description": "distinct-comment",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "distinct-comment-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "coll0"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "coll0",
31+
"databaseName": "distinct-comment-tests",
32+
"documents": [
33+
{
34+
"_id": 1,
35+
"x": 11
36+
},
37+
{
38+
"_id": 2,
39+
"x": 22
40+
},
41+
{
42+
"_id": 3,
43+
"x": 33
44+
}
45+
]
46+
}
47+
],
48+
"tests": [
49+
{
50+
"description": "distinct with document comment",
51+
"runOnRequirements": [
52+
{
53+
"minServerVersion": "4.4.14"
54+
}
55+
],
56+
"operations": [
57+
{
58+
"name": "distinct",
59+
"object": "collection0",
60+
"arguments": {
61+
"fieldName": "x",
62+
"filter": {},
63+
"comment": {
64+
"key": "value"
65+
}
66+
},
67+
"expectResult": [ 11, 22, 33 ]
68+
}
69+
],
70+
"expectEvents": [
71+
{
72+
"client": "client0",
73+
"events": [
74+
{
75+
"commandStartedEvent": {
76+
"command": {
77+
"distinct": "coll0",
78+
"key": "x",
79+
"query": {},
80+
"comment": {
81+
"key": "value"
82+
}
83+
},
84+
"commandName": "distinct",
85+
"databaseName": "distinct-comment-tests"
86+
}
87+
}
88+
]
89+
}
90+
]
91+
},
92+
{
93+
"description": "distinct with string comment",
94+
"runOnRequirements": [
95+
{
96+
"minServerVersion": "4.4.0"
97+
}
98+
],
99+
"operations": [
100+
{
101+
"name": "distinct",
102+
"object": "collection0",
103+
"arguments": {
104+
"fieldName": "x",
105+
"filter": {},
106+
"comment": "comment"
107+
},
108+
"expectResult": [ 11, 22, 33 ]
109+
}
110+
],
111+
"expectEvents": [
112+
{
113+
"client": "client0",
114+
"events": [
115+
{
116+
"commandStartedEvent": {
117+
"command": {
118+
"distinct": "coll0",
119+
"key": "x",
120+
"query": {},
121+
"comment": "comment"
122+
},
123+
"commandName": "distinct",
124+
"databaseName": "distinct-comment-tests"
125+
}
126+
}
127+
]
128+
}
129+
]
130+
},
131+
{
132+
"description": "distinct with document comment - pre 4.4, server error",
133+
"runOnRequirements": [
134+
{
135+
"minServerVersion": "3.6.0",
136+
"maxServerVersion": "4.4.13"
137+
}
138+
],
139+
"operations": [
140+
{
141+
"name": "distinct",
142+
"object": "collection0",
143+
"arguments": {
144+
"fieldName": "x",
145+
"filter": {},
146+
"comment": {
147+
"key": "value"
148+
}
149+
},
150+
"expectError": {
151+
"isClientError": false
152+
}
153+
}
154+
],
155+
"expectEvents": [
156+
{
157+
"client": "client0",
158+
"events": [
159+
{
160+
"commandStartedEvent": {
161+
"command": {
162+
"distinct": "coll0",
163+
"key": "x",
164+
"query": {},
165+
"comment": {
166+
"key": "value"
167+
}
168+
},
169+
"commandName": "distinct",
170+
"databaseName": "distinct-comment-tests"
171+
}
172+
}
173+
]
174+
}
175+
]
176+
}
177+
]
178+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
description: "distinct-comment"
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name distinct-comment-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 11 }
23+
- { _id: 2, x: 22 }
24+
- { _id: 3, x: 33 }
25+
26+
tests:
27+
- description: "distinct with document comment"
28+
runOnRequirements:
29+
# https://jira.mongodb.org/browse/SERVER-44847
30+
# Server supports distinct with comment of any type for comment starting from 4.4.14.
31+
- minServerVersion: "4.4.14"
32+
operations:
33+
- name: distinct
34+
object: *collection0
35+
arguments:
36+
fieldName: &fieldName x
37+
filter: &filter {}
38+
comment: &documentComment { key: "value"}
39+
expectResult: [ 11, 22, 33 ]
40+
expectEvents:
41+
- client: *client0
42+
events:
43+
- commandStartedEvent:
44+
command:
45+
distinct: *collection0Name
46+
key: *fieldName
47+
query: *filter
48+
comment: *documentComment
49+
commandName: distinct
50+
databaseName: *database0Name
51+
52+
- description: "distinct with string comment"
53+
runOnRequirements:
54+
- minServerVersion: "4.4.0"
55+
operations:
56+
- name: distinct
57+
object: *collection0
58+
arguments:
59+
fieldName: *fieldName
60+
filter: *filter
61+
comment: &stringComment "comment"
62+
expectResult: [ 11, 22, 33 ]
63+
expectEvents:
64+
- client: *client0
65+
events:
66+
- commandStartedEvent:
67+
command:
68+
distinct: *collection0Name
69+
key: *fieldName
70+
query: *filter
71+
comment: *stringComment
72+
commandName: distinct
73+
databaseName: *database0Name
74+
75+
- description: "distinct with document comment - pre 4.4, server error"
76+
runOnRequirements:
77+
- minServerVersion: "3.6.0"
78+
maxServerVersion: "4.4.13"
79+
operations:
80+
- name: distinct
81+
object: *collection0
82+
arguments:
83+
fieldName: *fieldName
84+
filter: *filter
85+
comment: *documentComment
86+
expectError:
87+
isClientError: false
88+
expectEvents:
89+
- client: *client0
90+
events:
91+
- commandStartedEvent:
92+
command:
93+
distinct: *collection0Name
94+
key: *fieldName
95+
query: *filter
96+
comment: *documentComment
97+
commandName: distinct
98+
databaseName: *database0Name

mongo/collection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,13 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
11361136
if option.Collation != nil {
11371137
op.Collation(bsoncore.Document(option.Collation.ToDocument()))
11381138
}
1139+
if option.Comment != nil {
1140+
comment, err := transformValue(coll.registry, option.Comment, true, "comment")
1141+
if err != nil {
1142+
return nil, err
1143+
}
1144+
op.Comment(comment)
1145+
}
11391146
if option.MaxTime != nil {
11401147
op.MaxTimeMS(int64(*option.MaxTime / time.Millisecond))
11411148
}

mongo/integration/unified/collection_operation_execution.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ func executeDistinct(ctx context.Context, operation *operation) (*operationResul
437437
return nil, fmt.Errorf("error creating collation: %v", err)
438438
}
439439
opts.SetCollation(collation)
440+
case "comment":
441+
opts.SetComment(val)
440442
case "fieldName":
441443
fieldName = val.StringValue()
442444
case "filter":

mongo/options/distinctoptions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type DistinctOptions struct {
1515
// default value is nil, which means the default collation of the collection will be used.
1616
Collation *Collation
1717

18+
// A string or document that will be included in server logs, profiling logs, and currentOp queries to help trace
19+
// the operation. The default value is nil, which means that no comment will be included in the logs.
20+
Comment interface{}
21+
1822
// The maximum amount of time that the query can run on the server. The default value is nil, meaning that there
1923
// is no time limit for query execution.
2024
//
@@ -35,6 +39,12 @@ func (do *DistinctOptions) SetCollation(c *Collation) *DistinctOptions {
3539
return do
3640
}
3741

42+
// SetComment sets the value for the Comment field.
43+
func (do *DistinctOptions) SetComment(comment interface{}) *DistinctOptions {
44+
do.Comment = comment
45+
return do
46+
}
47+
3848
// SetMaxTime sets the value for the MaxTime field.
3949
//
4050
// Deprecated: This option is deprecated and will eventually be removed in version 2.0 of the driver. The more general
@@ -56,6 +66,9 @@ func MergeDistinctOptions(opts ...*DistinctOptions) *DistinctOptions {
5666
if do.Collation != nil {
5767
distinctOpts.Collation = do.Collation
5868
}
69+
if do.Comment != nil {
70+
distinctOpts.Comment = do.Comment
71+
}
5972
if do.MaxTime != nil {
6073
distinctOpts.MaxTime = do.MaxTime
6174
}

x/mongo/driver/operation/distinct.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"time"
1313

14+
"go.mongodb.org/mongo-driver/bson/bsontype"
1415
"go.mongodb.org/mongo-driver/event"
1516
"go.mongodb.org/mongo-driver/mongo/description"
1617
"go.mongodb.org/mongo-driver/mongo/readconcern"
@@ -29,6 +30,7 @@ type Distinct struct {
2930
session *session.Client
3031
clock *session.ClusterClock
3132
collection string
33+
comment bsoncore.Value
3234
monitor *event.CommandMonitor
3335
crypt driver.Crypt
3436
database string
@@ -114,6 +116,9 @@ func (d *Distinct) command(dst []byte, desc description.SelectedServer) ([]byte,
114116
}
115117
dst = bsoncore.AppendDocumentElement(dst, "collation", d.collation)
116118
}
119+
if d.comment.Type != bsontype.Type(0) {
120+
dst = bsoncore.AppendValueElement(dst, "comment", d.comment)
121+
}
117122
if d.key != nil {
118123
dst = bsoncore.AppendStringElement(dst, "key", *d.key)
119124
}
@@ -196,6 +201,16 @@ func (d *Distinct) Collection(collection string) *Distinct {
196201
return d
197202
}
198203

204+
// Comment sets a value to help trace an operation.
205+
func (d *Distinct) Comment(comment bsoncore.Value) *Distinct {
206+
if d == nil {
207+
d = new(Distinct)
208+
}
209+
210+
d.comment = comment
211+
return d
212+
}
213+
199214
// CommandMonitor sets the monitor to use for APM events.
200215
func (d *Distinct) CommandMonitor(monitor *event.CommandMonitor) *Distinct {
201216
if d == nil {

0 commit comments

Comments
 (0)