MongoDB Go Driver Tutorial
MongoDB Go Driver
The official MongoDB Go Driver recently moved to GA with the release of version 1.0.0. It’s now regarded as feature complete and ready for production use. This tutorial will help you get started with the MongoDB Go Driver. You will create a simple program and learn how to:
- Install the MongoDB Go Driver
- Connect to MongoDB using the Go Driver
- Use BSON objects in Go
- Send CRUD operations to MongoDB
You can view the complete code for this tutorial on this GitHub repository. In order to follow along, you will need a MongoDB database to which you can connect. You can use a MongoDB database running locally, or easily create a free 500 MB database using MongoDB Atlas.
Install the MongoDB Go Driver
The MongoDB Go Driver is made up of several packages. If you are just using go get, you can install the driver using:
go get go.mongodb.org/mongo-driver
The output of this may look like a warning stating something like package go.mongodb.org/mongo-driver: no Go files in (…). This is expected output from go get.
If you are using the
dep package manager, you can install the main mongo package as well as the bson and mongo/options package using this command:
1 2 3
dep ensure --add go.mongodb.org/mongo-driver/mongo \ go.mongodb.org/mongo-driver/bson \ go.mongodb.org/mongo-driver/mongo/options