Skip to content

GODRIVER-3892 Move count to the mongo package.#2483

Open
matthewdale wants to merge 2 commits into
mongodb:masterfrom
matthewdale:godriver3892-move-count
Open

GODRIVER-3892 Move count to the mongo package.#2483
matthewdale wants to merge 2 commits into
mongodb:masterfrom
matthewdale:godriver3892-move-count

Conversation

@matthewdale

@matthewdale matthewdale commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

GODRIVER-3892

Summary

Move the count operation logic from the x/mongo/driver/operation package to the mongo package, removing the unnecessary chainable setters. Don't export the moved types.

Background & Motivation

Part of a larger effort to reduce unnecessary code by removing the x/mongo/driver/operation package.

@mongodb-drivers-pr-bot

Copy link
Copy Markdown
Contributor

API Change Report

./v2/x/mongo/driver/operation

incompatible changes

Count: removed
CountResult: removed
NewCount: removed

@mongodb-drivers-pr-bot

mongodb-drivers-pr-bot Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

🧪 Performance Results

Commit SHA: 908dbd0

The following benchmark tests for version 6a566e47d624a600077830dd had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkSingleFindOneByID ops_per_second_min -45.6190 671.4983 Avg: 1234.8039
Med: 1243.7811
Stdev: 207.1143
0.8132 -2.7198
BenchmarkMultiInsertSmallDocument total_mem_allocs -15.3358 2276663.0000 Avg: 2689049.0000
Med: 2689049.0000
Stdev: 15600.1898
0.9866 -26.4347
BenchmarkMultiInsertSmallDocument total_time_seconds -11.9168 1.0300 Avg: 1.1694
Med: 1.1694
Stdev: 0.0078
0.9803 -17.9604
BenchmarkBSONFlatDocumentEncoding ops_per_second_max -3.6932 81076.6986 Avg: 84185.8256
Med: 84139.6772
Stdev: 1145.5324
0.7940 -2.7141
BenchmarkMultiFindMany ops_per_second_med 0.9801 3676470.5882 Avg: 3640787.4364
Med: 3636363.6364
Stdev: 7662.2464
0.9174 4.6570
BenchmarkLargeDocInsertOne allocated_bytes_per_op -0.2896 5677.0000 Avg: 5693.4891
Med: 5695.0000
Stdev: 5.1302
0.8621 -3.2142

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@matthewdale matthewdale added review-priority-low Low Priority PR for Review: within 3 business days ignore-for-release labels Jul 11, 2026
@matthewdale matthewdale force-pushed the godriver3892-move-count branch from 6e1c991 to 969385a Compare July 14, 2026 03:10
@matthewdale matthewdale added review-priority-normal Medium Priority PR for Review: within 1 business day and removed review-priority-low Low Priority PR for Review: within 3 business days labels Jul 14, 2026
@matthewdale matthewdale marked this pull request as ready for review July 14, 2026 04:40
@matthewdale matthewdale requested a review from a team as a code owner July 14, 2026 04:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR moves the internal “count” operation implementation out of x/mongo/driver/operation and into the mongo package, simplifying EstimatedDocumentCount by removing the chainable setter style and keeping the moved types unexported.

Changes:

  • Removed the legacy Count operation implementation from x/mongo/driver/operation.
  • Added an unexported countOp implementation in mongo/ to execute the count command and parse results.
  • Updated Collection.EstimatedDocumentCount to construct and run countOp directly, setting comment/rawData fields without setters.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
x/mongo/driver/operation/count.go Removes the previous Count operation implementation from the x/ operations layer.
mongo/op_count.go Adds the new unexported countOp + result parsing logic in the mongo package.
mongo/collection.go Switches EstimatedDocumentCount from operation.NewCount() to the new countOp struct literal wiring.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mongo/op_count.go Outdated
Comment thread mongo/op_count.go
Comment thread mongo/op_count.go
Comment on lines +69 to +80
case "cursor": // for count using aggregate with $collStats
firstBatch, err := element.Value().Document().LookupErr("firstBatch")
if err != nil {
return cr, err
}

// get count value from first batch
val := firstBatch.Array().Index(0)
count, err := val.Document().LookupErr("n")
if err != nil {
return cr, err
}
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread mongo/op_count.go
cr.N, ok = count.AsInt64OK()
if !ok {
return cr, fmt.Errorf("response field 'n' is type int64, but received BSON type %s",
element.Value().Type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]

Suggested change
element.Value().Type)
count.Type)

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

Labels

ignore-for-release review-priority-normal Medium Priority PR for Review: within 1 business day

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants