BLOGAtlas Vector Search voted most loved vector database in 2024 Retool State of AI report — Read more >>

What is Golang?

Golang, or the Go programming language, was developed at Google and publicly announced in 2009. It is now an open source project with many contributors from the open source community.

The combination of Go's compiled performance and its lightweight, data-friendly syntax make it a perfect match for building data-driven applications with MongoDB. Go is one of the newest programming languages that MongoDB supports through the official Go Driver.

If you're looking to create a high-performance native application but don't relish the idea of using C or C++, Go and MongoDB are for you.

Why use MongoDB with Go?

Go is meant to be simple, yet powerful. Its rich language, which feels like a dynamically typed interpreted language, is compiled to static machine code quickly. The code runs natively on different operating systems and hardware specs.

Go is easy to write, and its multicore concurrency helps get the most out of hardware. It's expressive, flexible, and modular. Because it's statically compiled to machine code, its runtime power is increased. It's extremely fast, interoperable, and convenient.

MongoDB holds the top spot as the most popular NoSQL database technology on several lists. It is a document database used to build highly available and scalable internet applications. Founded in 2007, MongoDB has a worldwide following in the developer community. MongoDB has always focused on providing developers with an excellent user experience, which, in addition to all its other features, has made MongoDB a favorite of developers worldwide.

If you're building something new and need a database, consider using MongoDB Atlas from the outset. Atlas will give you a fully-managed, cloud-native database service that comes with a number of features, including full-text search, charts, partner integrations, and much more. If you're building for iOS or Android, Atlas Device Sync makes mobile synchronization a snap. You can even build web applications on GraphQL directly on top of MongoDB Atlas.

MongoDB's flexibility makes it an ideal pairing with Go for building servers and data processing pipelines alike.

Getting started with Go and MongoDB

MongoDB supports the Go programming language through the official MongoDB Go Driver! The Go Driver lets you connect to and communicate with MongoDB clusters from a Go application. The MongoDB driver supports all of the newest features of MongoDB, including multi-document transactions, client side encryption, bulk operations, and aggregation for advanced analytics cases. Working with MongoDB documents in Go is similar to working with JSON or XML.

To get started, you'll need to have Go installed on your system. To add the MongoDB driver as a dependency, you can use go get:

go get

You can use go get to get any additional dependencies you might need.

Using the Go Driver to connect to MongoDB Atlas

The MongoDB Go Driver needs instructions on where and how to connect to your MongoDB cluster. These instructions are stored in the connection string, which includes information on hostname or IP address and port of your cluster, authentication mechanism, credentials where applicable, and other connection options.

To retrieve your connection string for your cluster and user, log into your Atlas account, navigate to the Database section, and click the Connect button for the cluster that you want to connect.

Proceed to the Connect Your Application step, select "Go" as the MongoDB driver and the driver version, and then copy the string.

You can use this connection string in your project to connect to MongoDB with the example code.

Documents and Collections

Unlike SQL databases, MongoDB stores information in documents and collections.


MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. Documents are composed of field-value pairs, with the following structure:

    field1: value1,   
    field2: value2,   
    field3: value3,   
    fieldN: valueN

The values associated with fields can be any BSON data type, including other documents, arrays, and even arrays of documents. Field names themselves are strings.

Documents are much more flexible than standard rows in relational databases, as they give you a dynamic schema rather than an enforced one (though you can do schema validation with MongoDB if you want!).


MongoDB stores documents in collections. A collection is like a table in a relational database. For more details on how to create a collection, and other information about collections, you can check out the documentation here.

CRUD operations with MongoDB and Go

CRUD (create, read, update, and delete) operations are the four basic operations you might want to do with a database.

In the case of MongoDB, you can use CRUD operations to insert documents, write queries for matching documents, update documents, and delete documents. You can do this by using the MongoDB Query API. The query API simplifies working with data in your application code, and is easy to use for both simple and advanced operations.

Create documents

To insert a single document, you can use the InsertOne() method. See our example of creating and inserting a document into a collection. If you'd like to insert multiple documents, you can use InsertMany(). View the sample code to insert a single document, as well as to insert multiple documents.

Reading documents

To find the single document that you inserted using InsertOne(), you can use the FindOne() method. It'll return a single document. To find multiple documents at once, you can use the Find() method.

Delete documents

You can remove documents from your MongoDB collections using delete operations. You may have heard of the deleteOne method already. Similar to the methods above that insert documents, you can use either deleteOne() for one document or deleteMany() for multiple documents. Both the deleteOne method and the deleteMany method return a DeleteResult type that contains information on the number of documents deleted.

Updating documents

To change documents in MongoDB, you can use either update or replace operations. Update operations change the fields that you specify while leaving all other fields and values unchanged. Replace operations remove all existing fields -- save for _id -- and substitute the deleted fields with the new fields and values you specify.

There are several methods to change documents that the official driver provides: updateOne() for one document, updateMany() for multiple documents, ReplaceOne(), BulkWrite(), FindOneAndReplace(), and so many others. View more information on updating and replacing documents.

Modeling documents with Go data structures

There are many ways to interact with data through the MongoDB Go driver operations. One way is to map document fields to native Go data structures. Being able to work with data directly how you'd find it in the database is a huge benefit!

Find a full tutorial on mapping document fields to native Go data structures using the MongoDB Go driver.

Client-side encryption in Go

One of the many great things about MongoDB is its security features. In addition to network and user-based rules, you can encrypt your data at-rest, over the wire, and now, using client-side field level encryption (CSFLE).

With field level encryption, you can choose to encrypt certain fields within a document, client-side, while leaving other fields as plain text. This is particularly useful because when viewing a CSFLE document with the CLI, Compass, or directly within Altas, the encrypted fields will not be human readable. When they are not human readable, if the documents should get into the wrong hands, those fields will be useless to the malicious user. However, when using the MongoDB language drivers while using the same encryption keys, those fields can be decrypted and are queryable within the application.

A great tutorial on CSFLE in MongoDB using the Go Driver can be found on Developer Center.

Using Go and MongoDB with other technologies

The official MongoDB Go Driver allows you to integrate MongoDB into any application and tap into the impressive Go ecosystem. There are numerous ways to create applications in Go to leverage MongoDB:

Server development

For server development, there are full-featured web frameworks like Revel and Gin, as well as more lightweight tools like Web.go. There are even simpler modular approaches like the Gorilla web toolkit.

Data science

For data science use cases, you can use tools like Gopher Data, Gopher Notes, and Gota or qframe.

Machine learning

For machine learning use cases, GoLearn is a possibility.


MongoDB University is a great place to find information on all things MongoDB. There are official certification programs as well as more informal developer paths to guide your learning journey.

The MongoDB Developer Center contains a wealth of information on the MongoDB Go Driver, including tutorials, videos, and quickstarts, as well as sample code.

Getting help

The best part of using MongoDB is the vibrant Go community that includes users with all levels of experience with the Go driver. The best way to get support for general questions is to use MongoDB Community Forums or StackOverflow.

If you're running into an unexpected error, you think you've found a bug in the Go driver, or you have a feature request, please open a JIRA ticket on the GODRIVER project. You can find instructions on how to do this in the documentation. Bug reports for both the Go driver and the Core Server are public.


Can I contribute to the Go Driver?

Yes! We are happy to accept contributions to help improve the driver. We will guide user contributions to ensure they meet the standards of the codebase. Please ensure that any pull requests include documentation, tests, and pass gradle checks.

To get started, check out the source code and work on a branch. Find more information on submitting pull requests.

Where can I find additional tutorials using the MongoDB Go Driver?

You can find additional examples using the MongoDB Go Driver in the MongoDB Developer Center. The Developer Center has tutorials, videos, and code examples to help you build with MongoDB.

Where can I find documentation for the Go driver?

You can also consult the MongoDB Go Driver documentation for usage examples, including fully runnable code snippets, as well as a quick reference guide for common MongoDB commands. There's information on change streams, BSON documents and BSON objects, and so much more.

What if I run into specific errors or need help using the MongoDB Go driver?

Check out the MongoDB Community Forums or StackOverflow!