{"id":13412367,"url":"https://github.com/mongodb/mongo-go-driver","last_synced_at":"2026-01-28T22:02:26.528Z","repository":{"id":37334327,"uuid":"81355383","full_name":"mongodb/mongo-go-driver","owner":"mongodb","description":"The Official Golang driver for MongoDB","archived":false,"fork":false,"pushed_at":"2025-05-06T15:27:56.000Z","size":45331,"stargazers_count":8324,"open_issues_count":13,"forks_count":905,"subscribers_count":123,"default_branch":"master","last_synced_at":"2025-05-06T16:17:01.084Z","etag":null,"topics":["database","driver","go","golang","golang-library","mongodb"],"latest_commit_sha":null,"homepage":"https://www.mongodb.com/docs/drivers/go/current/","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongodb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-02-08T17:18:02.000Z","updated_at":"2025-05-06T09:31:07.000Z","dependencies_parsed_at":"2025-05-05T22:59:12.027Z","dependency_job_id":"fb1c9ef7-5d4e-456b-902c-f6d901a0393f","html_url":"https://github.com/mongodb/mongo-go-driver","commit_stats":{"total_commits":2142,"total_committers":161,"mean_commits":"13.304347826086957","dds":0.8169934640522876,"last_synced_commit":"b823409f1094fc54099bf64799a1e5a1d9e6fa67"},"previous_names":["10gen/mongo-go-driver"],"tags_count":142,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-go-driver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-go-driver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-go-driver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-go-driver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongodb","download_url":"https://codeload.github.com/mongodb/mongo-go-driver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990456,"owners_count":21995773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","driver","go","golang","golang-library","mongodb"],"created_at":"2024-07-30T20:01:23.928Z","updated_at":"2026-01-21T22:05:02.353Z","avatar_url":"https://github.com/mongodb.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"etc/assets/mongo-gopher.png\" width=\"250\"\u003e\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://goreportcard.com/report/go.mongodb.org/mongo-driver/v2\"\u003e\u003cimg src=\"https://goreportcard.com/badge/go.mongodb.org/mongo-driver/v2\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo\"\u003e\u003cimg src=\"etc/assets/godev-mongo-blue.svg\" alt=\"docs\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson\"\u003e\u003cimg src=\"etc/assets/godev-bson-blue.svg\" alt=\"docs\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.mongodb.com/docs/drivers/go/current/\"\u003e\u003cimg src=\"etc/assets/docs-mongodb-green.svg\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://securityscorecards.dev/viewer/?uri=github.com/mongodb/mongo-go-driver\"\u003e\n    \u003cimg src=\"https://api.securityscorecards.dev/projects/github.com/mongodb/mongo-go-driver/badge\" alt=\"OpenSSF Scorecard\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n# MongoDB Go Driver\n\nThe MongoDB supported driver for Go.\n\nSee the following resources to learn more about upgrading from version 1.x to 2.0.:\n\n- [v2.0 Migration Guide](docs/migration-2.0.md)\n- [v2.0 What's New](https://www.mongodb.com/docs/drivers/go/upcoming/whats-new/#what-s-new-in-2.0)\n\nThe MongoDB Go driver follows [semantic versioning](https://semver.org/) for its releases.\n\n## Requirements\n\n- Go 1.19 or higher. We aim to support the latest versions of Go.\n- Go 1.25 or higher is required to run the driver test suite.\n- MongoDB 4.2 and higher.\n\n## Installation\n\nThe recommended way to get started using the MongoDB Go driver is by using Go modules to install the dependency in\nyour project. This can be done either by importing packages from `go.mongodb.org/mongo-driver` and having the build\nstep install the dependency or by explicitly running\n\n```bash\ngo get go.mongodb.org/mongo-driver/v2/mongo\n```\n\nWhen using a version of Go that does not support modules, the driver can be installed using `dep` by running\n\n```bash\ndep ensure -add \"go.mongodb.org/mongo-driver/v2/mongo\"\n```\n\n## Usage\n\nTo get started with the driver, import the `mongo` package and create a `mongo.Client` with the `Connect` function:\n\n```go\nimport (\n    \"context\"\n    \"time\"\n\n    \"go.mongodb.org/mongo-driver/v2/mongo\"\n    \"go.mongodb.org/mongo-driver/v2/mongo/options\"\n    \"go.mongodb.org/mongo-driver/v2/mongo/readpref\"\n)\n\nclient, _ := mongo.Connect(options.Client().ApplyURI(\"mongodb://localhost:27017\"))\n```\n\nMake sure to defer a call to `Disconnect` after instantiating your client:\n\n```go\ndefer func() {\n    if err := client.Disconnect(ctx); err != nil {\n        panic(err)\n    }\n}()\n```\n\nFor more advanced configuration and authentication, see the [documentation for mongo.Connect](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Connect).\n\nCalling `Connect` does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to,\nuse the `Ping` method:\n\n```go\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\ndefer cancel()\n\n_ = client.Ping(ctx, readpref.Primary())\n```\n\nTo insert a document into a collection, first retrieve a `Database` and then `Collection` instance from the `Client`:\n\n```go\ncollection := client.Database(\"testing\").Collection(\"numbers\")\n```\n\nThe `Collection` instance can then be used to insert documents:\n\n```go\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\n\nres, _ := collection.InsertOne(ctx, bson.D{{\"name\", \"pi\"}, {\"value\", 3.14159}})\nid := res.InsertedID\n```\n\nTo use `bson.D`, you will need to add `\"go.mongodb.org/mongo-driver/v2/bson\"` to your imports.\n\nYour import statement should now look like this:\n\n```go\nimport (\n    \"context\"\n    \"log\"\n    \"time\"\n\n    \"go.mongodb.org/mongo-driver/v2/bson\"\n    \"go.mongodb.org/mongo-driver/v2/mongo\"\n    \"go.mongodb.org/mongo-driver/v2/mongo/options\"\n    \"go.mongodb.org/mongo-driver/v2/mongo/readpref\"\n)\n```\n\nSeveral query methods return a cursor, which can be used like this:\n\n```go\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\n\ncur, err := collection.Find(ctx, bson.D{})\nif err != nil {\n  log.Fatal(err)\n}\n\ndefer cur.Close(ctx)\nfor cur.Next(ctx) {\n    var result bson.D\n    if err := cur.Decode(\u0026result); err != nil {\n      log.Fatal(err)\n    }\n\n    // do something with result....\n}\n\nif err := cur.Err(); err != nil {\n    log.Fatal(err)\n}\n```\n\nFor methods that return a single item, a `SingleResult` instance is returned:\n\n```go\nvar result struct {\n    Value float64\n}\n\nfilter := bson.D{{\"name\", \"pi\"}}\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\n\nerr := collection.FindOne(ctx, filter).Decode(\u0026result)\nif errors.Is(err, mongo.ErrNoDocuments) {\n    // Do something when no record was found\n} else if err != nil {\n    log.Fatal(err)\n}\n\n// Do something with result...\n```\n\nAdditional examples and documentation can be found under the examples directory and [on the MongoDB Documentation website](https://www.mongodb.com/docs/drivers/go/current/).\n\n### Network Compression\n\nNetwork compression will reduce bandwidth requirements between MongoDB and the application.\n\nThe Go Driver supports the following compression algorithms:\n\n1. [Snappy](https://google.github.io/snappy/) (`snappy`): available in MongoDB 3.4 and later.\n1. [Zlib](https://zlib.net/) (`zlib`): available in MongoDB 3.6 and later.\n1. [Zstandard](https://github.com/facebook/zstd/) (`zstd`): available in MongoDB 4.2 and later.\n\n#### Specify Compression Algorithms\n\nCompression can be enabled using the `compressors` parameter on the connection string or by using [`ClientOptions.SetCompressors`](https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo/options#ClientOptions.SetCompressors):\n\n```go\nopts := options.Client().ApplyURI(\"mongodb://localhost:27017/?compressors=snappy,zlib,zstd\")\nclient, _ := mongo.Connect(opts)\n```\n\n```go\nopts := options.Client().SetCompressors([]string{\"snappy\", \"zlib\", \"zstd\"})\nclient, _ := mongo.Connect(opts)\n```\n\nIf compressors are set, the Go Driver negotiates with the server to select the first common compressor. For server configuration and defaults, refer to [`networkMessageCompressors`](https://www.mongodb.com/docs/manual/reference/program/mongod/#std-option-mongod.--networkMessageCompressors).\n\nMessages compress when both parties enable network compression; otherwise, messages remain uncompressed\n\n## Support / Feedback\n\nFor issues with, questions about, or feedback for the Go Driver, please look into our [support channels](https://www.mongodb.com/docs/manual/support/), including [StackOverflow](https://stackoverflow.com/questions/tagged/mongodb%20go?sort=Newest).\n\nNew features and bugs can be reported on the [GODRIVER Jira project](https://jira.mongodb.org/browse/GODRIVER).\n\n## Contribution\n\nCheck out the [GODRIVER Jira project](https://jira.mongodb.org/browse/GODRIVER) for tickets that need completing. See our [contribution guidelines](docs/CONTRIBUTING.md) for details.\n\n## Continuous Integration\n\nCommits to master are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-go-driver).\n\n## Frequently Encountered Issues\n\nSee our [common issues](docs/common-issues.md) documentation for troubleshooting frequently encountered issues.\n\n## Thanks and Acknowledgement\n\n- The Go Gopher artwork by [@ashleymcnamara](https://github.com/ashleymcnamara)\n- The original Go Gopher was designed by [Renee French](http://reneefrench.blogspot.com/)\n\n## License\n\nThe MongoDB Go Driver is licensed under the [Apache License](LICENSE).\n","funding_links":[],"categories":["Popular","Misc","Go","开源类库","Middleware","Database Drivers","Open source library","Data Management \u0026 Processing","数据库管理系统","Libraries","数据库驱动`连接和操作数据库工具`","数据库驱动程序","Data Integration Frameworks","数据库驱动","Generators","NoSQL Databases"],"sub_categories":["数据库","Advanced Console UIs","Database","Database \u0026 Cloud Management","网络服务_其他","NoSQL Database Drivers","Go","SQL 查询语句构建库","NoSQL数据库驱动程序"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongodb%2Fmongo-go-driver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongodb%2Fmongo-go-driver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongodb%2Fmongo-go-driver/lists"}