MongoDB Go Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

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.")

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

See Usage for more detail.

You can use the Versioned API feature starting with MongoDB Server version 5.0 and Go Driver version 1.6. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned API:

// Replace <connection string> with your MongoDB deployment's connection string.
uri := "<connection string>"
ctx := context.TODO()
// Set the version of the Versioned API on the client.
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOptions)

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.
  2. Install and configure MongoDB Server.
  3. Start the server.

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

Go Driver Version
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
[1] The 1.6 driver does not support snapshot reads on secondaries. For more information, see the MongoDB Server version 5.0 release notes.

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
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility