Skip to content

Driver v2: Distinct returns a DistinctResult that must be decoded #5

Description

@TopherGopher

Part of the easymongo refactor (see PLAN.md on the refactor branch).

Working context (read this first)

Scope

  • find_query.go line 344 (mongoColl.Distinct(ctx, field, filter, opts)) returned ([]interface{}, error) in v1. In v2 it returns *mongo.DistinctResult with Decode(v any) error and Err().
  • Update find_distinct.go so the easymongo Distinct(...) API keeps its current signature for callers (whatever it returns today, keep returning that by decoding into it). If the current API returns []interface{}, decode into []any; also add a typed variant DistinctInto(field string, result any) error so callers can decode straight into []string etc.
  • Remove the commented-out v1 code at line 221.

Implementation details

Verified in v2.9.0:

func (coll *mongo.Collection) Distinct(ctx context.Context, fieldName string, filter any, opts ...options.Lister[options.DistinctOptions]) *mongo.DistinctResult
func (dr *mongo.DistinctResult) Decode(v any) error
func (dr *mongo.DistinctResult) Err() error
func (dr *mongo.DistinctResult) Raw() (bson.RawArray, error)

Implementation shape in find_distinct.go:

res := coll.mongoColl.Distinct(ctx, field, filter, opts)
if err := res.Err(); err != nil { return nil, err }
var out []any
if err := res.Decode(&out); err != nil { return nil, err }
return out, nil

DistinctInto(field string, result any) error calls res.Decode(result) directly; result must be a pointer to a slice, document that.

Tests first (find_distinct_test.go)

  • Distinct over name on the Batman archive fixture returns the six names.
  • DistinctInto decodes into []string.
  • Distinct with a filter that matches nothing returns an empty slice, not an error.
  • Distinct honouring Collation still works (case-insensitive collation collapses Joker/joker).

Acceptance

  • Existing distinct tests pass; new ones added.

Depends on: #4. Lands in the same pull request as #2, #3 and #4.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions