This file provides guidance to AI agents when working with code in this repository.
Official MongoDB Go Driver (go.mongodb.org/mongo-driver/v2). Correctness, backward compatibility, and conformance to MongoDB Driver Specifications are top priorities.
- Module:
go.mongodb.org/mongo-driver/v2 - Issue tracker: JIRA GODRIVER project — GitHub Issues is not used for bugs/features
- Active major version: v2 on
master. v1 lives onrelease/1.xbranches.
Uses Task, not make.
task # default: build + check-license + check-fmt + check-modules + lint + test-short
task fmt # gofumpt -w . — use this, not plain gofmt
task build # includes compilecheck-119 (Go 1.19 compat)
task lint # golangci-lint across linux/{386,arm,arm64,amd64,ppc64le,s390x}
task test-short # race detector, ~60s timeout — fast feedback
task test # full suite, serial (-p 1), 1800s timeout, requires mongod
task api-report # required when public API changes — include output in PR description
Tests require a running mongod on localhost:27017 (or MONGODB_URI). The full test suite is serial (-p 1) — integration tests share server state. Do not parallelize.
- Go 1.19: minimum to compile/use the driver. Public packages (
mongo/,bson/,event/,tag/) must build on 1.19.compilecheck-119enforces this at build time. - Go 1.25+: required to run the test suite and develop the driver.
- Avoid
slices,maps,cmp,errors.Join,min/maxbuiltins, and other post-1.19 stdlib in non-internal/code.
x/packages are importable but not semver-covered. Users do importx/mongo/driveretc. Flag breaking changes explicitly in PR descriptions even though semver doesn't require it.- Atomic alignment on 32-bit.
int64/uint64fields accessed viasync/atomicmust be at struct start or useatomic.Int64. The lint pass onGOARCH=386catches this — don't skip cross-arch lint. - GridFS lives in
mongo/. v2 mergedgridfs.Bucketintomongo.GridFSBucket. Don't resurrect the oldgridfsimport path. - CSOT timeouts. Use context deadlines; do not introduce per-call wall-clock timers that race with the user's context.
- Command monitoring redacts credentials. When adding commands that handle credentials (
authenticate,saslStart, etc.), add them to the redaction list. libmongocryptrequired for CSFLE/QE tests. Install withtask install-libmongocrypt. CSFLE code requires thecsebuild tag.
- Formatter:
gofumpt(stricter superset of gofmt). Always usetask fmt, never plain gofmt. - Errors:
fmt.Errorf("…: %w", err)for wrapping. Useerrors.Is/errors.Asfor comparisons — neverstrings.Containson error messages. Sentinel errors live in themongopackage (mongo.ErrNoDocuments, etc.). - Context: Every blocking public method takes
context.Contextas its first argument. Usemongo.SessionFromContext(ctx)(v2 pattern) — not the oldmongo.SessionContext. - BSON:
bson.Dfor ordered documents and commands.bson.Monly when order genuinely doesn't matter.bsoncore.Documentin hot paths (zero-copy, byte-slice-backed). - Exported names: Full words, not abbreviations (
ClientBulkWriteResult, notCBWResult). Doc comments required on all exported identifiers using the "name is subject" convention. - New options: Follow the setter pattern:
func (o *XxxOptionsBuilder) SetFoo(v T) *XxxOptionsBuilder.
- Use
internal/assert(wraps testify) for assertions. Preferassert.ErrorIsoverassert.ErrorContains. - Integration tests: use
internal/integration/mtestfor topology-aware setup, fail-point injection, and event capture. - Spec tests: JSON/YAML fixtures under
testdata/(git submodule — runtask init-submoduleafter cloning if missing). - Prose tests: number them to match the spec (e.g.,
"3. bulkWrite batch splits…") for cross-referencing. - Unit tests live next to the code (
foo.go↔foo_test.go). Integration tests live ininternal/integration/.
- JIRA prefix required:
GODRIVER-1234 Short descriptionfor all commit messages and PR titles. - Features →
master. Bug fixes → latest stable release branch (e.g.,release/2.5). The "Merge up" GitHub Action propagates fixes to newer branches automatically. - Run
task(default target) before every PR. - Changed public API (
mongo/,bson/,event/,tag/)? Runtask api-reportand include the output in the PR description. - User-visible breaking change? Update
docs/migration-2.0.md. - Spec compliance changes? Quote the relevant spec requirement in the PR description.