Skip to content

feat(NODE-6494): add support for hint on distinct commands #4487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/operations/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ import { CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

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

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

if (options.hint != null) {
cmd.hint = options.hint;
}

// Do we have a readConcern specified
decorateWithReadConcern(cmd, coll, options);

Expand Down
139 changes: 139 additions & 0 deletions test/spec/crud/unified/distinct-hint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"description": "distinct-hint",
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "7.1.0"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "distinct-hint-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "distinct-hint-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
],
"tests": [
{
"description": "distinct with hint string",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": "_id_"
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": "_id_"
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
},
{
"description": "distinct with hint document",
"operations": [
{
"name": "distinct",
"object": "collection0",
"arguments": {
"fieldName": "x",
"filter": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"expectResult": [
11
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"distinct": "coll0",
"key": "x",
"query": {
"_id": 1
},
"hint": {
"_id": 1
}
},
"commandName": "distinct",
"databaseName": "distinct-hint-tests"
}
}
]
}
]
}
]
}
73 changes: 73 additions & 0 deletions test/spec/crud/unified/distinct-hint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
description: "distinct-hint"

schemaVersion: "1.0"
runOnRequirements:
# https://jira.mongodb.org/browse/SERVER-14227
# Server supports distinct with hint starting from 7.1.0.
- minServerVersion: "7.1.0"

createEntities:
- client:
id: &client0 client0
observeEvents: [ commandStartedEvent ]
- database:
id: &database0 database0
client: *client0
databaseName: &database0Name distinct-hint-tests
- collection:
id: &collection0 collection0
database: *database0
collectionName: &collection0Name coll0

initialData:
- collectionName: *collection0Name
databaseName: *database0Name
documents:
- { _id: 1, x: 11 }
- { _id: 2, x: 22 }
- { _id: 3, x: 33 }

tests:
- description: "distinct with hint string"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: &fieldName x
filter: &filter { _id: 1 }
hint: _id_
expectResult: [ 11 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
hint: _id_
commandName: distinct
databaseName: *database0Name

- description: "distinct with hint document"
operations:
- name: distinct
object: *collection0
arguments:
fieldName: *fieldName
filter: *filter
hint:
_id: 1
expectResult: [ 11 ]
expectEvents:
- client: *client0
events:
- commandStartedEvent:
command:
distinct: *collection0Name
key: *fieldName
query: *filter
hint:
_id: 1
commandName: distinct
databaseName: *database0Name