Skip to content

Commit 40d0e87

Browse files
feat(NODE-6494): add support for hint on distinct commands (#4487)
1 parent d01ecc7 commit 40d0e87

File tree

3 files changed

+230
-1
lines changed

3 files changed

+230
-1
lines changed

src/operations/distinct.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ import { CommandOperation, type CommandOperationOptions } from './command';
88
import { Aspect, defineAspects } from './operation';
99

1010
/** @public */
11-
export type DistinctOptions = CommandOperationOptions;
11+
export type DistinctOptions = CommandOperationOptions & {
12+
/**
13+
* @sinceServerVersion 7.1
14+
*
15+
* The index to use. Specify either the index name as a string or the index key pattern.
16+
* If specified, then the query system will only consider plans using the hinted index.
17+
*
18+
* If provided as a string, `hint` must be index name for an index on the collection.
19+
* If provided as an object, `hint` must be an index description for an index defined on the collection.
20+
*
21+
* See https://www.mongodb.com/docs/manual/reference/command/distinct/#command-fields.
22+
*/
23+
hint?: Document | string;
24+
};
1225

1326
/**
1427
* Return a list of distinct values for the given key across a collection.
@@ -71,6 +84,10 @@ export class DistinctOperation extends CommandOperation<any[]> {
7184
cmd.comment = options.comment;
7285
}
7386

87+
if (options.hint != null) {
88+
cmd.hint = options.hint;
89+
}
90+
7491
// Do we have a readConcern specified
7592
decorateWithReadConcern(cmd, coll, options);
7693

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"description": "distinct-hint",
3+
"schemaVersion": "1.0",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "7.1.0"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client0",
13+
"observeEvents": [
14+
"commandStartedEvent"
15+
]
16+
}
17+
},
18+
{
19+
"database": {
20+
"id": "database0",
21+
"client": "client0",
22+
"databaseName": "distinct-hint-tests"
23+
}
24+
},
25+
{
26+
"collection": {
27+
"id": "collection0",
28+
"database": "database0",
29+
"collectionName": "coll0"
30+
}
31+
}
32+
],
33+
"initialData": [
34+
{
35+
"collectionName": "coll0",
36+
"databaseName": "distinct-hint-tests",
37+
"documents": [
38+
{
39+
"_id": 1,
40+
"x": 11
41+
},
42+
{
43+
"_id": 2,
44+
"x": 22
45+
},
46+
{
47+
"_id": 3,
48+
"x": 33
49+
}
50+
]
51+
}
52+
],
53+
"tests": [
54+
{
55+
"description": "distinct with hint string",
56+
"operations": [
57+
{
58+
"name": "distinct",
59+
"object": "collection0",
60+
"arguments": {
61+
"fieldName": "x",
62+
"filter": {
63+
"_id": 1
64+
},
65+
"hint": "_id_"
66+
},
67+
"expectResult": [
68+
11
69+
]
70+
}
71+
],
72+
"expectEvents": [
73+
{
74+
"client": "client0",
75+
"events": [
76+
{
77+
"commandStartedEvent": {
78+
"command": {
79+
"distinct": "coll0",
80+
"key": "x",
81+
"query": {
82+
"_id": 1
83+
},
84+
"hint": "_id_"
85+
},
86+
"commandName": "distinct",
87+
"databaseName": "distinct-hint-tests"
88+
}
89+
}
90+
]
91+
}
92+
]
93+
},
94+
{
95+
"description": "distinct with hint document",
96+
"operations": [
97+
{
98+
"name": "distinct",
99+
"object": "collection0",
100+
"arguments": {
101+
"fieldName": "x",
102+
"filter": {
103+
"_id": 1
104+
},
105+
"hint": {
106+
"_id": 1
107+
}
108+
},
109+
"expectResult": [
110+
11
111+
]
112+
}
113+
],
114+
"expectEvents": [
115+
{
116+
"client": "client0",
117+
"events": [
118+
{
119+
"commandStartedEvent": {
120+
"command": {
121+
"distinct": "coll0",
122+
"key": "x",
123+
"query": {
124+
"_id": 1
125+
},
126+
"hint": {
127+
"_id": 1
128+
}
129+
},
130+
"commandName": "distinct",
131+
"databaseName": "distinct-hint-tests"
132+
}
133+
}
134+
]
135+
}
136+
]
137+
}
138+
]
139+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
description: "distinct-hint"
2+
3+
schemaVersion: "1.0"
4+
runOnRequirements:
5+
# https://jira.mongodb.org/browse/SERVER-14227
6+
# Server supports distinct with hint starting from 7.1.0.
7+
- minServerVersion: "7.1.0"
8+
9+
createEntities:
10+
- client:
11+
id: &client0 client0
12+
observeEvents: [ commandStartedEvent ]
13+
- database:
14+
id: &database0 database0
15+
client: *client0
16+
databaseName: &database0Name distinct-hint-tests
17+
- collection:
18+
id: &collection0 collection0
19+
database: *database0
20+
collectionName: &collection0Name coll0
21+
22+
initialData:
23+
- collectionName: *collection0Name
24+
databaseName: *database0Name
25+
documents:
26+
- { _id: 1, x: 11 }
27+
- { _id: 2, x: 22 }
28+
- { _id: 3, x: 33 }
29+
30+
tests:
31+
- description: "distinct with hint string"
32+
operations:
33+
- name: distinct
34+
object: *collection0
35+
arguments:
36+
fieldName: &fieldName x
37+
filter: &filter { _id: 1 }
38+
hint: _id_
39+
expectResult: [ 11 ]
40+
expectEvents:
41+
- client: *client0
42+
events:
43+
- commandStartedEvent:
44+
command:
45+
distinct: *collection0Name
46+
key: *fieldName
47+
query: *filter
48+
hint: _id_
49+
commandName: distinct
50+
databaseName: *database0Name
51+
52+
- description: "distinct with hint document"
53+
operations:
54+
- name: distinct
55+
object: *collection0
56+
arguments:
57+
fieldName: *fieldName
58+
filter: *filter
59+
hint:
60+
_id: 1
61+
expectResult: [ 11 ]
62+
expectEvents:
63+
- client: *client0
64+
events:
65+
- commandStartedEvent:
66+
command:
67+
distinct: *collection0Name
68+
key: *fieldName
69+
query: *filter
70+
hint:
71+
_id: 1
72+
commandName: distinct
73+
databaseName: *database0Name

0 commit comments

Comments
 (0)