mongo-go-driver is a free, open source databases project written in Go and released under Apache-2.0. It has 8,538 GitHub stars, 938 forks and 26 open issues, and was last pushed 5 hours ago. On this registry it ranks #102 of 203 tracked projects in Databases, with 5 head-to-head comparisons available.

What is mongo-go-driver?

mongo-go-driver is the MongoDB-supported, official Go driver that Go applications use to connect to MongoDB 4.4 and later, written for Go developers and teams building services on top of MongoDB.

What it is

mongo-go-driver is the official MongoDB driver for the Go language, published as the Go module go.mongodb.org/mongo-driver, with version 2 packages imported from go.mongodb.org/mongo-driver/v2/mongo. It lives in the Go ecosystem and is distributed through Go modules, so projects either import the packages and let the build step resolve the dependency or fetch it explicitly. The project is licensed under Apache-2.0, publishes releases under semantic versioning, and carries the topics database, driver, go, golang, golang-library, and mongodb.

The concrete problem it solves is providing a supported client library for talking to MongoDB from Go, rather than leaving applications to assemble connection management, BSON value handling, and query result iteration on their own. The README documentation covers that surface directly: mongo.Connect with options.Client().ApplyURI establishes a client, Database and Collection give access to data, bson.D carries documents, and cursors are walked with cur.Next and decoded with cur.Decode. For existing users, version 2 supersedes the 1.x line, and the upgrade path is documented in docs/migration-2.0.md alongside a "What's New in 2.0" page.

Key capabilities

  • Client creation with mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017")), paired with a deferred client.Disconnect(ctx) call to release resources.
  • Server discovery confirmation through client.Ping(ctx, readpref.Primary()), since Connect does not block waiting for discovery.
  • Document writes and reads with collection.InsertOne, collection.Find, and collection.FindOne, using bson.D filters such as bson.D{{"name", "pi"}}.
  • Cursor iteration returning multiple results, using cur.Next(ctx), cur.Decode(&result), cur.Close(ctx), and cur.Err().
  • Single-result handling through SingleResult and the mongo.ErrNoDocuments sentinel, which signals that no record matched.
  • Network compression between application and server with snappy (MongoDB 3.4+), zlib (MongoDB 3.6+), and zstd.
  • Supported versions of Go 1.25 or higher, covering the last two Go minor versions, with Go 1.26 or higher required to run the driver test suite, against MongoDB 4.4 and higher.

Who uses it and how

  • Go services deployed against MongoDB 4.4 or newer that need a driver maintained by MongoDB itself rather than an unsupported client.
  • Teams upgrading from version 1.x to 2.0, following docs/migration-2.0.md and the "What's New in 2.0" documentation.
  • Applications with bandwidth-sensitive traffic between application and database, selecting one of the supported compression algorithms at connection configuration time.
  • Projects that track Go releases closely, since the driver follows the last two Go minor versions and needs Go 1.25+ to build, with Go 1.26+ for the test suite.
  • Developers onboarding to the API through the examples directory and the MongoDB Go driver documentation site at https://www.mongodb.com/docs/drivers/go/current/.

Getting started

Install with Go modules by running go get go.mongodb.org/mongo-driver/v2/mongo, or by importing the mongo, options, bson, and readpref packages and letting the build step fetch the dependency. After installation, create a client with mongo.Connect, then Ping it before issuing queries.

How it compares

The provided facts list no paid products that this project replaces, and they name no comparable alternative drivers. On the evidence available here, mongo-go-driver stands alone in this registry.

When to use it — and when not to

The driver is a client library only: it does not provision, host, or operate a MongoDB deployment, so anyone adopting it still needs a reachable MongoDB 4.4 or newer server and a Go 1.25 or newer toolchain. Teams on earlier Go versions, or on MongoDB releases below 4.4, cannot use this version. Shops whose applications are not written in Go should look elsewhere, since the entire surface here is Go packages.

project readme (upstream, from github) — read inline

OpenSSF Scorecard

MongoDB Go Driver

The MongoDB supported driver for Go.

See the following resources to learn more about upgrading from version 1.x to 2.0.:

The MongoDB Go Driver follows semantic versioning for its releases.

Requirements

  • Go 1.25 or higher. The Go Driver supports the last two Go minor versions.
  • Go 1.26 or higher is required to run the driver test suite.
  • MongoDB 4.4 and higher.

Installation

The recommended way to get started using the MongoDB Go Driver is by using Go modules to install the dependency in your project. This can be done either by importing packages from go.mongodb.org/mongo-driver and having the build step install the dependency or by explicitly running

go get go.mongodb.org/mongo-driver/v2/mongo

Usage

To get started with the driver, import the mongo package and create a mongo.Client with the Connect function:

import (
    "context"
    "time"

    "go.mongodb.org/mongo-driver/v2/mongo"
    "go.mongodb.org/mongo-driver/v2/mongo/options"
    "go.mongodb.org/mongo-driver/v2/mongo/readpref"
)

client, _ := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))

Make sure to defer a call to Disconnect after instantiating your client:

defer func() {
    if err := client.Disconnect(ctx); err != nil {
        panic(err)
    }
}()

For more advanced configuration and authentication, see the documentation for mongo.Connect.

Calling Connect does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to, use the Ping method:

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

_ = client.Ping(ctx, readpref.Primary())

To insert a document into a collection, first retrieve a Database and then Collection instance from the Client:

collection := client.Database("testing").Collection("numbers")

The Collection instance can then be used to insert documents:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, _ := collection.InsertOne(ctx, bson.D{{"name", "pi"}, {"value", 3.14159}})
id := res.InsertedID

To use bson.D, you will need to add "go.mongodb.org/mongo-driver/v2/bson" to your imports.

Your import statement should now look like this:

import (
    "context"
    "log"
    "time"

    "go.mongodb.org/mongo-driver/v2/bson"
    "go.mongodb.org/mongo-driver/v2/mongo"
    "go.mongodb.org/mongo-driver/v2/mongo/options"
    "go.mongodb.org/mongo-driver/v2/mongo/readpref"
)

Several query methods return a cursor, which can be used like this:

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

cur, err := collection.Find(ctx, bson.D{})
if err != nil {
  log.Fatal(err)
}

defer cur.Close(ctx)
for cur.Next(ctx) {
    var result bson.D
    if err := cur.Decode(&result); err != nil {
      log.Fatal(err)
    }

    // do something with result....
}

if err := cur.Err(); err != nil {
    log.Fatal(err)
}

For methods that return a single item, a SingleResult instance is returned:

var result struct {
    Value float64
}

filter := bson.D{{"name", "pi"}}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

err := collection.FindOne(ctx, filter).Decode(&result)
if errors.Is(err, mongo.ErrNoDocuments) {
    // Do something when no record was found
} else if err != nil {
    log.Fatal(err)
}

// Do something with result...

Additional examples and documentation can be found under the examples directory and on the MongoDB Documentation website.

Network Compression

Network compression will reduce bandwidth requirements between MongoDB and the application.

The Go Driver supports the following compression algorithms:

  1. Snappy (snappy): available in MongoDB 3.4 and later.
  2. Zlib (zlib): available in MongoDB 3.6 and later.
  3. Zstandard (zstd): available in MongoDB 4.2 and later.
Specify Compression Algorithms

Compression can be enabled using the compressors parameter on the connection string or by using ClientOptions.SetCompressors:

opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")
client, _ := mongo.Connect(opts)
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})
client, _ := mongo.Connect(opts)

If compressors are set, the Go Driver negotiates with the server to select the first common compressor. For server configuration and defaults, refer to networkMessageCompressors.

Messages compress when both parties enable network compression; otherwise, messages remain uncompressed

Support / Feedback

For issues with, questions about, or feedback for the Go Driver, please look into our support channels, including StackOverflow.

New features and bugs can be reported on the GODRIVER Jira project.

Contribution

Check out the GODRIVER Jira project for tickets that need completing. See our contribution guidelines for details.

Continuous Integration

Commits to master are run automatically on evergreen.

Frequently Encountered Issues

See our common issues documentation for troubleshooting frequently encountered issues.

Thanks and Acknowledgement

License

The MongoDB Go Driver is licensed under the Apache License.

Frequently asked questions

Is mongo-go-driver free to use?

mongo-go-driver is open source under the Apache-2.0 licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does mongo-go-driver do?

The Official Golang driver for MongoDB

What is mongo-go-driver written in?

mongo-go-driver is primarily written in Go. Its source is publicly available at https://github.com/mongodb/mongo-go-driver, and it has 8,538 GitHub stars.