From e44b70688abe8027173649da8740eaeb5e97ef8e Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:25:37 -0800 Subject: [PATCH] Correct the SetHint docs and return an error if the hint value is an multi-key map. --- mongo/collection.go | 3 +++ mongo/options/distinctoptions.go | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/mongo/collection.go b/mongo/collection.go index 0e38c060db..300f0c34e1 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -1282,6 +1282,9 @@ func (coll *Collection) Distinct( op.Comment(comment) } if args.Hint != nil { + if isUnorderedMap(args.Hint) { + return &DistinctResult{err: ErrMapForOrderedArgument{"hint"}} + } hint, err := marshalValue(args.Hint, coll.bsonOpts, coll.registry) if err != nil { return &DistinctResult{err: err} diff --git a/mongo/options/distinctoptions.go b/mongo/options/distinctoptions.go index b9415e7d56..0b9167d5d0 100644 --- a/mongo/options/distinctoptions.go +++ b/mongo/options/distinctoptions.go @@ -62,10 +62,14 @@ func (do *DistinctOptionsBuilder) SetComment(comment interface{}) *DistinctOptio return do } -// SetHint sets the value for the Comment field. Specifies a string or document that -// will be included in server logs, profiling logs, and currentOp queries to help trace -// the operation. The default value is nil, which means that no comment will be included -// in the logs. +// SetHint specifies the index to use for the operation. This should either be +// the index name as a string or the index specification as a document. This +// option is only valid for MongoDB versions >= 7.1. Previous server versions +// will return an error if an index hint is specified. Distinct returns an error +// if the hint parameter is a multi-key map. The default value is nil, which +// means that no index hint will be sent. +// +// SetHint sets the Hint field. func (do *DistinctOptionsBuilder) SetHint(hint interface{}) *DistinctOptionsBuilder { do.Opts = append(do.Opts, func(opts *DistinctOptions) error { opts.Hint = hint