Skip to content

Commit 753ecfe

Browse files
fix(NODE-4273): pass 'comment' option through to distinct command (#3339)
1 parent c70c69e commit 753ecfe

File tree

4 files changed

+337
-10
lines changed

4 files changed

+337
-10
lines changed

src/operations/distinct.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
3-
import { MongoCompatibilityError } from '../error';
43
import type { Server } from '../sdam/server';
54
import type { ClientSession } from '../sessions';
6-
import { Callback, decorateWithCollation, decorateWithReadConcern, maxWireVersion } from '../utils';
5+
import { Callback, decorateWithCollation, decorateWithReadConcern } from '../utils';
76
import { CommandOperation, CommandOperationOptions } from './command';
87
import { Aspect, defineAspects } from './operation';
98

@@ -61,6 +60,12 @@ export class DistinctOperation extends CommandOperation<any[]> {
6160
cmd.maxTimeMS = options.maxTimeMS;
6261
}
6362

63+
// we check for undefined specifically here to allow falsy values
64+
// eslint-disable-next-line no-restricted-syntax
65+
if (typeof options.comment !== 'undefined') {
66+
cmd.comment = options.comment;
67+
}
68+
6469
// Do we have a readConcern specified
6570
decorateWithReadConcern(cmd, coll, options);
6671

@@ -71,13 +76,6 @@ export class DistinctOperation extends CommandOperation<any[]> {
7176
return callback(err);
7277
}
7378

74-
if (this.explain && maxWireVersion(server) < 4) {
75-
callback(
76-
new MongoCompatibilityError(`Server ${server.name} does not support explain on distinct`)
77-
);
78-
return;
79-
}
80-
8179
super.executeCommand(server, session, cmd, (err, result) => {
8280
if (err) {
8381
callback(err);

test/integration/node-specific/comment_with_falsy_values.test.ts

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Long } from '../../../src';
1+
import { expect } from 'chai';
2+
3+
import { Collection, CommandStartedEvent, Long, MongoClient } from '../../../src';
24
import { TestBuilder, UnifiedTestSuiteBuilder } from '../../tools/utils';
35

46
const falsyValues = [0, false, '', Long.ZERO, null, NaN] as const;
@@ -173,4 +175,47 @@ describe('Comment with falsy values', () => {
173175
.test(testsForChangeStreamsAggregate)
174176
.test(testsForGetMore)
175177
.run();
178+
179+
context('Collection.distinct()', function () {
180+
let client: MongoClient;
181+
let collection: Collection;
182+
let commands: CommandStartedEvent[] = [];
183+
184+
beforeEach(async function () {
185+
client = this.configuration.newClient({ monitorCommands: true });
186+
client.on('commandStarted', e => commands.push(e));
187+
await client.connect();
188+
collection = await client.db('comment-falsy-values').createCollection('collection');
189+
commands = [];
190+
});
191+
192+
afterEach(async function () {
193+
await collection.drop();
194+
await client.close();
195+
});
196+
197+
for (const falsyValue of falsyValues) {
198+
it(`distinct should send falsy value ${falsyToString(
199+
falsyValue
200+
)} on the command`, async function () {
201+
await collection.distinct('some-key', {}, { comment: falsyValue }).catch(() => null);
202+
203+
expect(commands).to.have.lengthOf(1);
204+
const distinctCommand = commands.find(command => command.commandName === 'distinct');
205+
expect(distinctCommand).to.exist;
206+
207+
// chai does not narrow types, so TS doesn't know the distinct command exists at this point.
208+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
209+
const command = distinctCommand!.command;
210+
211+
expect(command).to.haveOwnProperty('comment');
212+
213+
if (Number.isNaN(falsyValue)) {
214+
expect(command.comment).to.be.NaN;
215+
} else {
216+
expect(command.comment).to.equal(falsyValue);
217+
}
218+
});
219+
}
220+
});
176221
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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": [
68+
11,
69+
22,
70+
33
71+
]
72+
}
73+
],
74+
"expectEvents": [
75+
{
76+
"client": "client0",
77+
"events": [
78+
{
79+
"commandStartedEvent": {
80+
"command": {
81+
"distinct": "coll0",
82+
"key": "x",
83+
"query": {},
84+
"comment": {
85+
"key": "value"
86+
}
87+
},
88+
"commandName": "distinct",
89+
"databaseName": "distinct-comment-tests"
90+
}
91+
}
92+
]
93+
}
94+
]
95+
},
96+
{
97+
"description": "distinct with string comment",
98+
"runOnRequirements": [
99+
{
100+
"minServerVersion": "4.4.0"
101+
}
102+
],
103+
"operations": [
104+
{
105+
"name": "distinct",
106+
"object": "collection0",
107+
"arguments": {
108+
"fieldName": "x",
109+
"filter": {},
110+
"comment": "comment"
111+
},
112+
"expectResult": [
113+
11,
114+
22,
115+
33
116+
]
117+
}
118+
],
119+
"expectEvents": [
120+
{
121+
"client": "client0",
122+
"events": [
123+
{
124+
"commandStartedEvent": {
125+
"command": {
126+
"distinct": "coll0",
127+
"key": "x",
128+
"query": {},
129+
"comment": "comment"
130+
},
131+
"commandName": "distinct",
132+
"databaseName": "distinct-comment-tests"
133+
}
134+
}
135+
]
136+
}
137+
]
138+
},
139+
{
140+
"description": "distinct with document comment - pre 4.4, server error",
141+
"runOnRequirements": [
142+
{
143+
"minServerVersion": "3.6.0",
144+
"maxServerVersion": "4.4.13"
145+
}
146+
],
147+
"operations": [
148+
{
149+
"name": "distinct",
150+
"object": "collection0",
151+
"arguments": {
152+
"fieldName": "x",
153+
"filter": {},
154+
"comment": {
155+
"key": "value"
156+
}
157+
},
158+
"expectError": {
159+
"isClientError": false
160+
}
161+
}
162+
],
163+
"expectEvents": [
164+
{
165+
"client": "client0",
166+
"events": [
167+
{
168+
"commandStartedEvent": {
169+
"command": {
170+
"distinct": "coll0",
171+
"key": "x",
172+
"query": {},
173+
"comment": {
174+
"key": "value"
175+
}
176+
},
177+
"commandName": "distinct",
178+
"databaseName": "distinct-comment-tests"
179+
}
180+
}
181+
]
182+
}
183+
]
184+
}
185+
]
186+
}
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

0 commit comments

Comments
 (0)