Skip to content

feat: Upgrade to MongoDB Go Driver v2 (Resolves #95)#96

Open
sagargupta2001 wants to merge 3 commits intoKamva:masterfrom
sagargupta2001:feature/upgrade-mongo-v2
Open

feat: Upgrade to MongoDB Go Driver v2 (Resolves #95)#96
sagargupta2001 wants to merge 3 commits intoKamva:masterfrom
sagargupta2001:feature/upgrade-mongo-v2

Conversation

@sagargupta2001
Copy link
Copy Markdown

Resolves #95

Motivation
The official MongoDB Go driver has released v2 (go.mongodb.org/mongo-driver/v2), introducing breaking changes and new features. Projects upgrading to the v2 driver are currently blocked from using mgm due to type incompatibilities (e.g., *mongo/options.ClientOptions vs the new v2 types). This PR upgrades mgm's core dependency to the v2 driver to unblock downstream users and ensure long-term support.

Because the v2 driver introduces breaking API changes, this PR will likely warrant a major version bump for mgm (e.g., v4).

Summary of Changes

Dependency & Go Version: Bumped the minimum Go version to 1.22 (as the v2 driver requires 1.19+ and relies heavily on generics requiring 1.18+). Upgraded mongo-driver to v2.5.0.

Import Paths: Systematically updated all imports from go.mongodb.org/mongo-driver/... to go.mongodb.org/mongo-driver/v2/....

BSON Primitives: The bson/primitive package was merged into bson in the v2 driver. Updated all references across models and collections (e.g., primitive.ObjectID → bson.ObjectID).

Connection API: Updated connection.go. mongo.NewClient() was removed in v2; refactored setup to use mongo.Connect() directly.

Sessions & Transactions: Replaced the removed mongo.SessionContext with standard context.Context. Handled the shift of mongo.Session to a pointer *mongo.Session, and updated the mongo.WithSession callback and TransactionFunc types accordingly.

Options Pattern (Generics): Adapted to the v2 driver's split of options and introduction of the options.Lister[T] generic pattern. Updated all option references (e.g., *options.UpdateOptions → options.Lister[options.UpdateOneOptions]). Refactored UpsertTrueOption() to utilize the new builder pattern.

Breaking Changes for mgm Users
Downstream users upgrading to this version of mgm will need to:

Update their imports to use the v2 MongoDB driver.

Replace any usage of primitive.ObjectID in their models with bson.ObjectID.

Adjust any custom database option configurations to match the new v2 options.Lister generic types.

Testing Done

[x] Updated local MongoDB to run as a single-node replica set (to support Oplog for transaction tests).

[x] Ran the full mgm test suite locally (go test -v ./...) – all tests pass.

[x] Tested the local mgm build downstream in a separate Go project using the replace directive in go.mod to verify real-world compilation and usage.

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for MongoDB Go driver v2 (go.mongodb.org/mongo-driver/v2)

1 participant