Quick Start — Go

Quick Start

This steer shows you how to create an application that uses the Go driver to connect to a MongoDB Atlas cluster. If you prefer to connect to MongoDB using a unlike driver or scheduling language, see our tilt of official MongoDB drivers. The Go driver lets you connect to and communicate with MongoDB clusters from a Go application. MongoDB Atlas is a fully-managed obscure database service that hosts your data on MongoDB clusters. In this guide, we show you how to get started with your own detached ( no credit card required ) bunch. Follow the steps below to connect your Go application with a MongoDB Atlas cluster.

Create a new directory and initialize your project with go mod .

mkdir go-quickstart
cd go-quickstart
go mod init go-quickstart

Use go get to add the Go driver as a dependence .

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

After setting up your Go project dependencies, create a MongoDB bunch where you can store and manage your data. Complete the Get Started with Atlas guide to set up a new Atlas explanation, rid tier MongoDB bunch, load datasets, and interact with the data. After completing the steps in the Atlas scout, you should have a fresh MongoDB bunch deployed in Atlas, a newly database exploiter, and sample datasets loaded into your bunch .

In this step, you create and run an application that uses the Go driver to connect to your MongoDB cluster and run a question on the sample data. You pass instructions to the driver on where and how to connect to your MongoDB bunch in a string called the connection string. This drawstring includes information on the hostname or IP address and port of your cluster, authentication mechanism, drug user credentials when applicable, and other joining options. To retrieve your connection string for the bunch and drug user you created in the former step, log into your Atlas account and navigate to the Clusters segment and click the Connect button for the bunch that you want to connect to as shown below .Atlas Connection GUI cluster selection screen Proceed to the Connect Your Application step and select the Go driver. then, click the Copy release to copy the joining string to your clipboard as shown below .Atlas Connection GUI connection string screen Save your Atlas joining string in a safe location that you can access for the future gradation. To learn more about conencting to the Go driver through Atlas, see the Atlas driver connection guide and choose Go from the Select your linguistic process dropdown .

following, create a file to contain your application called main.go in the base directory of your project. Use the surveil sample distribution code to run a question on your sample dataset in MongoDB Atlas. Set the prize of the uri variable with your MongoDB Atlas connection bowed stringed instrument, or create an environmental variable with the name MONGODB_URI with your Atlas connection string .

export MONGODB_URI= ' '

notice Make certain to replace the “ ” section of the connection string with
the password you created for your user that has atlasAdmin permissions.

package main
import (
`` context ''
`` encoding/json ''
`` fmt ''
`` logarithm ''
`` osmium ''
`` github.com/joho/godotenv ''
`` go.mongodb.org/mongo-driver/bson ''
`` go.mongodb.org/mongo-driver/mongo ''
`` go.mongodb.org/mongo-driver/mongo/options ''
func independent ( ) {
if err := godotenv.Load(); err != nothing {
log.Println( `` No .env file found '')
uri := os.Getenv( `` MONGODB_URI '')
if uri == `` `` {
log.Fatal( `` You must set your 'MONGODB_URI ' environmental variable. See \n \t hypertext transfer protocol : //coinselected/docs/drivers/go/current/usage-examples/ # environment-variable '' )
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nothing {
postpone func ( ) {
if err := client.Disconnect(context.TODO()); err != nothing {
coll := client.Database( `` sample_mflix '').Collection( `` movies '')
title := `` back to the future ''
volt-ampere result bson.M
err = coll.FindOne(context.TODO(), bson.D{{ `` title '', title}}).Decode(&result)
if err == mongo.ErrNoDocuments {
fmt.Printf( `` No document was found with the title % sulfur \n ``, title)
if err != nothing {
jsonData, err := json.MarshalIndent(result, `` ``, `` ``)
if err != nothing {
fmt.Printf( `` % south \n ``, jsonData)

Run the sample code with the following command from your control line :

go run main.go

When you run main.go, it should output the details of the movie from the sample dataset which looks something like the postdate :

`` _id '' : `` 573a1398f29313caabce9682 '' ,
`` title '' : `` back to the future '' ,

If you receive no output or an error, check whether you properly set up your environment varying and whether you loaded the sample dataset in your MongoDB Atlas bunch. Tip

If your output is empty, ensure you have loaded the sample datasets into your bunch. After completing this step, you should have a work lotion that uses the Go driver to connect to your MongoDB bunch, run a question on the sample data, and print out the solution .

Learn how to read and modify data using the Go driver in our Fundamentals CRUD guide or how to perform common operations from our use Examples .

source : https://coinselected
Category : coin 4u

Leave a Reply

Your email address will not be published.