`hint` should be allowed with `distinct`

Open Beginner friendly
#142 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, mongodb, nodejs
Domain
database

Research direction

The denial lives in lib/permissions.js under denied.distinct.hint, and the JSDoc on Query.prototype.hint() repeats the restriction. Read _validate('hint') to see how the throw is reached, then check that Query.prototype.__distinct forwards hint through _optionsForExec(). Done when Model.distinct(...).hint({ a: 1 }) no longer throws against a 7.1+ server and the permissions test for distinct still passes.

Written by the indexing model from the issue text.

Description

lib/permissions.js lists hint among the options distinct can't take (denied.distinct.hint = true), so Query.prototype.hint() throws hint cannot be used with distinct via _validate('hint'). Mongoose routes every query option through mquery, so Model.distinct(field, filter).hint({ a: 1 }), or setOptions({ hint }) in a pre('distinct') hook, throws on the client before anything reaches the server.

The server's distinct command has accepted hint since MongoDB 7.1 (docs), and the Node driver's DistinctOptions.hint sends it (@sinceServerVersion 7.1). Mongoose's Query.prototype.__distinct already passes hint through _optionsForExec(), so removing the denial is enough.

Repro (mquery 6.0.1 / Mongoose 9.10.3, server 8.0):

await Model.distinct('field', { a: { $in: ids } }).hint({ a: 1 });
// Error: hint cannot be used with distinct

Proposed fix: remove denied.distinct.hint, and drop "Cannot be used with distinct()" from the hint() JSDoc. Servers older than 7.1 reject the field themselves, the same as other version-gated options. Happy to open the PR.

Dominant language
JavaScript
Stars
485
Forks
68
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mongoosejs/mquery

All issues in mongoosejs/mquery

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.