MongoDB Go Driver¶
Introduction¶
This is the official MongoDB Go Driver.
Installation¶
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 go.mongodb.org/mongo-driver/mongo
See Installation for additional ways to add the driver to your project.
Connect to MongoDB Atlas¶
To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:
package main import ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) 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 { panic(err) } defer func() { if err = client.Disconnect(ctx); err != nil { panic(err) } }() // Ping the primary if err := client.Ping(ctx, readpref.Primary()); err != nil { panic(err) } fmt.Println("Successfully connected and pinged.") }
See Usage for more detail.
Compatibility¶
MongoDB Compatibility¶
Go Driver | 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.4 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.3 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
1.2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
1.1 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
1.0 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Language Compatibility¶
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.
How to get help¶
- Ask questions on our MongoDB Community Forums.
- Visit our Support Channels.
- See the project JIRA to raise issues or request features.
Give Feedback