How to Use Go With MongoDB?

Topics Covered

Overview

MongoDB is a free and open-source NoSQL database. It is a document-oriented database that stores documents in a format called BSON (i.e. key-value pairs) that is comparable to JSON. MongoDB provides the notion of a collection of group documents.

Introduction

After depending on solutions created by the community for many years, MongoDB declared that they were developing a formal driver for Go. With the release of v1.0.0 in March 2019, this new driver was made production-ready, and updates have been made ever since.

The golang mongodb, like the other official MongoDB drivers, is idiomatic to the Go programming language and offers a simple mechanism for Go applications to use MongoDB as their database of choice. It is completely integrated with the MongoDB API and provides all its advanced features, including querying indexing, and aggregation. It will receive complete support from MongoDB engineers, unlike third-party libraries, so you can be assured of its continuous improvements and maintenance.

You'll learn how to use the official MongoDB Go Driver in this tutorial. Install the driver, establish a connection to a MongoDB database, and perform several CRUD activities. In the process, you'll develop a task manager application for command-line task management.

Prerequisite

You will require the following to follow and comprehend this tutorial:

  • MongoDB Installed on your system or a MongoDB Atlas account
  • Go 1.16 or greater installed on your functional computer
  • Your favourite IDE like VSCode

Installation

In this step, you will learn how to install golang MongoDB on your local machine. You can also use Mongo Atlas, a cloud instance of Mongodb. It has a free tier. For this tutorial, we will recommend using the mongo atlas.

Installation in the MacOS

brew command is the easiest way to install Mongodb in your locality. Make sure brew is installed on your MacOS machine. There are 2 options, either you can go to their website and download the package or follow the steps given below:

Installation in Windows

Golang mongodb installation in Windows is straightforward. Download the MSI installer file and then follow the instruction from the installer. Download Link

In the version dropdown, select the version of MongoDB to download. In the Platform dropdown, select Windows. In the Package dropdown, select msi, then, click on Download.

Run the MongoDB installer by double-clicking on the .msi file and doing as given in the image below.

install mongodb

When all of this is done, click on ready to install.

Installation in Linux

Open a new terminal, and run the below command.

There are multiple ways to install the mongodb in linux. Please checkout the download page for other options.

Using Mongo Atlas

You can use a Cloud MongoDB Solution known as MongoDB Atlas.

Create an Atlas account (you can sign in using your Google account), build a free cluster, add your IP to the list of permissible connections, create a database user for the cluster you've created, and connect to the cluster.

using mongo atlas

Once you've finished, we can move on to setting up your MongoDB database cluster connection using the MongoDB Go Driver.

Keep in mind that you must choose Connect to your application when connecting to your cluster, then copy the connection string from the following page and paste it into the code for your application. Replace the <password> with the user password.

connect to cluster

Install Mongo Driver

Create a new project directory gomongo. Open a new terminal and run the below command to initialise the project.

This command will initiate the project and create one go.mod file. This file is to track the dependencies used by the project.

Install the mongo-driver package.

Connect Go Driver with MongoDB

Now to connect the Go driver with MongoDB you need to follow the following steps:

  • Create mongo.Client with mongo.Connect function. The mongo.Client handles the connection with the MongoDB.
  • mongo.The client has a method called Ping which returns pong on a successful connection.
  • Finally, use mongo.Client.Disconnect to close the Database connection.

Output: output

Inserting Documents

To insert documents you need to follow the following steps:

  • Create mongo.Client with mongo.Connect function. The mongo.Client handles the connection with the MongoDB.
  • mongo.Client.The database returns a pointer type to the database.
  • Pointer to the database has method collection to select a collection to work with.
  • Collection type provides two methods to insert a document into MongoDB.
  • Collection.InsertOne() method can insert one document into the database.
  • Collection.InsertMany() method can insert a list of documents.
  • Then finally use mongo.Client.Disconnect to close the Database connection.

Output:

output for inserting

Finding Documents

The actions listed below must be taken to find documents:

  • Using mongo.Connect function, create a mongo.Client. The connection to MongoDB is managed by mongo.Client.
  • A pointer type to the database is returned by the database.
  • The database pointer has a collection method that allows you to choose a collection to work with.
  • For database queries, the collection has the Find() method.
  • Utilize mongo.Client next.
  • To terminate the database connection, disconnect.

Output:

output for finding documents

Updating Document

The actions below must be followed to update documents:

  • Using the mongo.Connect function, create a mongo.Client. The connection to MongoDB is managed by mongo.Client.
  • A pointer type to the database is returned by the database.
  • The database pointer has a collection method that allows you to choose a collection to work with.
  • Two ways to update the papers are provided by the collection.
  • Using the UpdateOne() function, one document that matches the query is modified.
  • The UpdateMany() method makes changes to all documents that match the search.
  • Utilize mongo.Client next.
  • To terminate the database connection, disconnect.

Output:

output for updating documnets

Deleting Documents

The steps below must be followed to delete a document:

  • Using mongo.Connect function, create a mongo.Client. The connection to MongoDB is managed by mongo.Client.
  • A pointer type to the database is returned by the database.
  • The database pointer has a collection method that allows you to choose a collection to work with.
  • There are two ways to delete documents from a collection using the collection.
  • The DeleteOne() function deletes just the one document that matches the search.
  • Every document that meets the query is deleted using the DeleteMany() function.
  • Utilize mongo.Client next.
  • To terminate the database connection, disconnect.

Output:

output for delete documents

Conclusion

  • An open-source NoSQL database is MongoDB. It is a document-oriented database that stores documents in a format similar to JSON called BSON (i.e. key-value pairs).
  • The golang mongodb driver, like the other official MongoDB drivers, is idiomatic to the Go programming language and offers a simple mechanism for Go applications to use MongoDB as their database of choice.
  • It is completely integrated with the MongoDB API and provides all its advanced features, including querying indexing, and aggregation. It will receive complete support from MongoDB engineers, unlike third-party libraries, so you can be assured of its continuous improvements and maintenance.