MongoDB Go Driver

This is the official MongoDB Go Driver.

The recommended way to get started using the MongoDB Go driver is by using Go modules

mkdir goproj
cd goproj
go mod init goproj
go get

See Installation for additional ways to add the driver to your project.

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

package main
import (
func main() {
// Replace the uri string with your MongoDB deployment's connection string.
uri := "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
defer func() {
if err = client.Disconnect(ctx); err != nil {
// Ping the primary
if err := client.Ping(ctx, readpref.Primary()); err != nil {
fmt.Println("Successfully connected and pinged.")

See Usage for more detail.

To connect to a database running locally on its default port, change the connection string to "mongodb://localhost".

To connect to a database running locally, change the connection string to "mongodb://localhost:<port>".

Your mongod instance must be running to successfully connect to your database. For information on how to start your mongod instance, see the Manage mongod Processes Server Manual Entry.

Go Driver Version
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6

The MongoDB Go driver requires Go 1.10 or later.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

Give Feedback

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Connect to localhost
  • Compatibility