AnnouncementIntroducing MongoDB 8.0, the fastest MongoDB ever! Read more >>Introducing MongoDB 8.0, the fastest MongoDB ever! >>

Golang is an expressive language built for modern-day scaled and distributed systems. It is a popular server-side language to build web applications and microservices, particularly those that need high performance and concurrent processing. Golang quickly became popular as it is simple to start with, and emerged as a reliable and flexible option to build large-scale systems.

MongoDB, with its developer data platform, is more than just a database. MongoDB is a fully-managed, multi-cloud platform that allows you to build and scale applications faster, perform advanced data analytics, visualize your data, and much more.


Table of contents

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, user-friendly syntax make it a perfect match for building data-driven applications with MongoDB. MongoDB supports the Go programming language 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.

A simple Golang code to add two numbers looks as follows:

package main 
import "fmt"

func main() {
    num1 := 1
    num2 := 2

    sum := num1 + num2
    fmt.Println("The sum is:", sum)
}

In the above example, we are importing the fmt (format) utility package and using it to perform the print operation. The main() is the main function of a Go program, i.e., the function that runs first when the application starts. As we see, the syntax is quite easy to read and understand. Go manages project dependencies using Go modules.

Golang is a compiled language, wherein the compiler converts the source code into machine code (executable binary) that can be run on any target platform that was used during compilation. Go does not need an interpreter. The Go runtime takes care of the memory management (garbage collection) and offers concurrency mechanisms (goroutines and channels).

An image of the elements of processing in Golang.

Unique Features of Golang

Some of the features that make Golang unique are:


Static typing and type inference

Go employs static typing—variable types must be known and are enforced by the compiler at compile time. This ensures code safety and catches any type errors in the code during compilation. Go also provides the “:=” operator, so even if the variable type is not explicitly defined, the compiler infers the type of variable based on its assigned value.


Concurrency

Golang introduces threads called goroutines that can execute multiple tasks concurrently. These goroutines can communicate and synchronize amongst themselves using channels and other synchronization primitives that Go provides, to ensure smooth task completion.


Garbage collection

Golang uses a tri-color mark and sweep garbage collector for efficient and automatic memory management. This also prevents many memory leaks in the application.


Golang structs

Structs (or structures) are an easy way to bind objects together. They are similar to classes in Java and structs in C++. Using structs, you can group similar features of an item together. For example, a customer can have fields like name, email, age, address, phone number, etc. These are common for all customers and hence can be used and stored together. MongoDB follows the same design principle—data that is used together is stored together. Here’s an example of a simple Golang struct:

package main 
import "fmt"

type Customer struct {
    Name    string
    Email   string
    Address string
    Age     int
    Phone   string
}

With this type of structure, it becomes easy to map the data model, and store and retrieve data. Structs are also why Go is a natural fit for MongoDB, as they are compatible with BSON. Go structs can be directly transformed into BSON documents and allow developers to specify how the fields should be encoded and decoded.


type Customer struct {
    Name    string `bson:"name"`
    Email   string `bson:"email"`
    Address string `bson:"address"`
    Age     int    `bson:"age"`
    Phone   string `bson:"phone"`
}

Why use MongoDB with Go?

Go is meant to be simple and light-weight, 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 a different operating system or hardware spec.

Go is easy to write, and its multi core 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.

For over a decade now, MongoDB has held the top spot as the most popular NoSQL database technology on several lists. You can have MongoDB installed on your machine, or use it anywhere via MongoDB Atlas. It is a developer data platform 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. MongoDB's flexibility makes it an ideal pairing with Go for building servers and data processing pipelines alike.

Since MongoDB server stores documents as BSON types, and Golang can directly map the fields with BSON types, it becomes very easy to move back and forth between Go structs and MongoDB documents.

Go structs also support more advanced configurations using tags like “omitempty” (to omit a field with zero value), “inline” (to have an embedded struct appear at the same level as the parent), “string” (encode a field as string, even when it is of a different type), and many more. This allows developers to customize and fine-tune the encoding-decoding of BSON/JSON documents in MongoDB.

MongoDB provides easy integration to Go with its official Go driver. The MongoDB Go driver provides an idiomatic way for interaction and supports all the CRUD operations and other advanced features.

An image describing why to use MongoDB and Go.

Getting started with Go and MongoDB Atlas

The official MongoDB Go driver lets you connect to and communicate with MongoDB clusters from a Go application. The MongoDB driver supports all the essential 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. You can create your first project using our quick start guide and add MongoDB driver as a dependency using go get:

go get go.mongodb.org/mongo-driver/mongo

An image of go and mongo driver code.


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

Using the go.mongodb.org driver

The MongoDB Go driver package comes with a set of tools to interact with the MongoDB database and perform a range of activities, from basic CRUD operations to advanced aggregations, including inserting, updating, and deleting multiple documents. The Go driver package also supports managing indexes, transactions, GridFS, change streams, and connection pooling.

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 the 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 MongoDB cluster that you want to connect.

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

Connecting Go application and MongoDB Atlas (connection string)

Getting the database connection

The Atlas UI displays the connection string that you can copy into your application code, and modify the user name and password for your application. The Atlas UI also gives you an option to view the full code sample showing how the MongoDB client instance uses the string to create a database connection instance to the MongoDB database.

If you are using any other tools, like MongoDB Compass or MongoDB Shell, you can select the corresponding option from the Atlas UI and use the given connection string in your project to connect to MongoDB Client. Check out the example application code, if you want to follow along.

Documents and collections

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


MongoDB documents

MongoDB databases store data records as BSON documents. BSON is a binary representation of JSON documents, though it supports more data types than JSON. MongoDB 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.

The following code shows how data is stored in MongoDB:

{
    "name": "Micheal Joe",
    "email": "mikej@example.com",
    "address": "1501, 9th Main, New Jersey, USA",
    "age": 43,
    "phone": "+1233377800"
}

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 database collection

MongoDB stores documents in collections. A database collection instance is like a table in a relational database. For more details on how to create a collection instance, and other information about collections, you can check out the documentation. While trying to insert documents, if MongoDB does not find the specified collection, it creates the collection and then inserts the documents.

CRUD operations with MongoDB and Go

Once you have installed Go and the MongoDB driver, you can query the MongoDB cluster from your application code.

The MongoDB database allows you to create, read and match, update, and delete documents. These operations are called CRUD (Create, Read, Update, and Delete) operations and are the four basic operations you might want to do with a database. MongoDB also provides options to execute advanced queries using its aggregation framework, from your application code.

You can do all of this 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.

CRUD operations via MongoDB Go driver.

Create (insert) single document

To insert a single document, you can use the InsertOne() method. See our example of creating and inserting a document into a collection.


Insert multiple documents

If you'd like to insert multiple documents, you can use InsertMany().


Reading documents

To find an inserted document using InsertOne(), you can use the FindOne() method. It'll return a single document. To find all the 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. Discover more information on updating and replacing documents.


Aggregation pipeline

You can create any type of query to get the exact documents that you need by creating a pipeline of aggregation operators and passing them to the Aggregate(pipeline) method. You can perform operations like filter, sort, group, and much more using MongoDB aggregation pipeline.

Modeling documents with Go data structure

There are many ways to interact with data through the MongoDB database methods. 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!

The Go programming language struct defines the schema of the MongoDB documents. The Go driver handles the conversion between BSON and Go types. The first step is to define the Go structs, and use the BSON tag to specify the mapping between the struct fields and MongoDB collection instance fields. Let’s go back to our earlier Go structs example of a “Customer” and see how we can map it to the MongoDB collection “customers” to ensure type safety. Let’s also add time and address fields in the following code:


// Address struct
type Address struct {
    Street string `bson:"street"`
    City   string `bson:"city"`
    State  string `bson:"state"`
    Zip    string `bson:"zip"`
}

// Customer struct
type Customer struct {
    ID      string    `bson:"_id",omitempty`
    Name    string    `bson:"name"`
    Email   string    `bson:"email"`
    Age     int       `bson:"age"`
    Phone   string    `bson:"phone"`
    Address Address   `bson:",inline"`
    Joining_date  time.Time `bson:"joining_date,omitempty"`
}

The ID field is left optional. However, it will be automatically handled by MongoDB in case no value is specified from the application.

The corresponding document in the MongoDB database will be:

{
    "_id": "603f2e4g8f1e6y7x8c0f9o1f",
    "name": "Micheal Joe",
    "email": "mikej@example.com",
    "age": 43,
    "phone": "+1233377800",
    "street": "1501, 9th Main",
    "city": "NY",
    "state": "NY",
    "zip": "12345",
    "joining_date": "2023-06-18T12:00:00Z"
}

Note that the address struct is marked “inline,” meaning that the embedded structure is stored in line with the customer structure and not as an embedded document. If the address is not marked inline, the document will look like:


{
    "_id": "603f2e4g8f1e6y7x8c0f9o1f",
    "name": "Micheal Joe",
    "email": "mikej@example.com",
    "age": 43,
    "phone": "+1233377800",
    "address": {
        "street": "1501, 9th Main",
        "city": "NY",
        "state": "NY",
        "zip": "12345",
    },
    "joining_date": "2023-06-18T12:00:00Z"
}

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 Atlas, 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 the 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.


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. These frameworks provide straightforward tools for cloud-based and server-side applications, and Go’s concurrent nature proves to be an ideal choice for high-performance server applications. You can also have very advanced implementations just using the standard library. The MongoDB database provides horizontal scalability, thus making it easier to build and maintain server applications, and handling large volumes of data.


Machine learning, artificial intelligence, and data science

For data science use cases, you can use tools like Gopher Data, Gopher Notes, and Gota or qframe. For machine learning use cases, GoLearn is a possibility.

Go’s highly performant nature is ideal for data-intensive tasks and large-scale data processing. The MongoDB database stores unstructured and semi-structured data easily due to its flexible dynamic schema. MongoDB also provides the aggregation framework which can easily handle complex scenarios through rich querying capabilities. Additionally, MongoDB offers features like vector search and supports full integration with large language model (LLM) and retrieval-augmented generation (RAG) tools.


Developing command line interface (CLI)

Go compiles to various operating systems easily and generates small, stand-alone (self-contained) binaries. The MongoDB database is the perfect back end for storage and retrieval of huge amounts of logs, configuration data, and other data required by the tool.


IoT, robotics, and gaming

IoT, robotics, and gaming systems require high-speed data processing as well as multitasking support. Both of these are supported by Go through its concurrent and highly performant nature. MongoDB’s flexible schema makes it extremely suitable to handle evolving data structures and high volumes of real-time data, like time-series data.


Microservices

Go provides fast deployment and startup time due to its small and self-contained binary size, thus making it an ideal choice for microservices. Go's ecosystem includes relevant tools such as protobuffers for faster and safer communication, loggers, identity provider clients, and other middleware to streamline development. Also, as MongoDB databases provide a flexible schema, each microservice can manage its own data model and easily store complex data structures in a collection instance.


Content management systems (CMS)

Content management applications require high speed concurrent request processing, which is one of the main selling points of Go. MongoDB complements this by providing a host of features like full-text search, content document versioning, and flexible data format, amongst others.


Real-time analytics

Go’s high performance and concurrency support along with MongoDB’s rich aggregation framework, real-time data handling, and indexing capabilities prove to be highly useful for data ingestion, processing, analysis, and transformation.


Common usecases of Go and MongoDB

MongoDB University tutorials

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 the MongoDB Community Forums or StackOverflow.

If you're running into an unexpected error, you think you've found a bug in the 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 driver and the Core Server are public.

FAQs

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 and tests and pass the required 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 on the MongoDB Developer Center. 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!