Skip to content

Commit ef1cb67

Browse files
terakilobyteiwysiukevinAlbs
authored
Docsp 13827 (#4)
* DOCSP-13827: add go quick-start * wording changes * nits * Update source/includes/quick-start/main.go Co-authored-by: Isabella Siu <[email protected]> * remove pointless else * comments * addressing comments * update query output * Update source/quick-start.txt Co-authored-by: Kevin Albertson <[email protected]> * add another note for sample dataset Co-authored-by: Isabella Siu <[email protected]> Co-authored-by: Kevin Albertson <[email protected]>
1 parent 5e82de3 commit ef1cb67

File tree

7 files changed

+169
-4
lines changed

7 files changed

+169
-4
lines changed
9.09 KB
Loading

source/includes/quick-start/atlas-setup.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ Atlas account, free tier MongoDB cluster, load datasets, and
88
interact with the data.
99

1010
After completing the steps in the Atlas guide, you should have a new MongoDB
11-
cluster deployed in Atlas, a new database user, and sample datasets loaded
12-
into your cluster.
11+
cluster deployed in Atlas, a new database user, and
12+
:atlas:`sample datasets loaded </sample-data/>` into your cluster.
1313

1414
Connect to your Cluster
1515
-----------------------
1616

17-
In this step, we create and run an application that uses the Go MongoDB
17+
In this step, you will create and run an application that uses the Go
1818
driver to connect to your MongoDB cluster and run a query on the sample
1919
data.
2020

21-
We pass instructions to the driver on where and how to connect to your
21+
You pass instructions to the driver on where and how to connect to your
2222
MongoDB cluster in a string called the *connection string*. This string
2323
includes information on the hostname or IP address and port of your
2424
cluster, authentication mechanism, user credentials when applicable, and
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"go.mongodb.org/mongo-driver/bson"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/mongo/options"
13+
)
14+
15+
func main() {
16+
ctx := context.TODO()
17+
uri := os.Getenv("MONGODB_URI")
18+
if uri == "" {
19+
log.Panic(`'uri' is empty. Ensure you set the 'MONGODB_URI'
20+
environment variable, or set the value of 'uri' to your
21+
Atlas connection string.`)
22+
}
23+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
24+
if err != nil {
25+
log.Panic(err)
26+
}
27+
28+
defer func() {
29+
if err := client.Disconnect(ctx); err != nil {
30+
log.Panic(err)
31+
}
32+
}()
33+
34+
coll := client.Database("sample_mflix").Collection("movies")
35+
title := "Back to the Future"
36+
37+
var result bson.M
38+
err = coll.FindOne(ctx, bson.D{{"title", title}}).Decode(&result)
39+
if err == mongo.ErrNoDocuments {
40+
fmt.Printf("No document was found with the title %s\n", title)
41+
return
42+
}
43+
if err != nil {
44+
log.Panic(err)
45+
}
46+
47+
jsonData, err := json.MarshalIndent(result, "", " ")
48+
if err != nil {
49+
log.Panic(err)
50+
}
51+
fmt.Printf("%s\n", jsonData)
52+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Learn how to read and modify data using the Go driver in our Fundamentals
2+
CRUD guide or how to perform common operations from our
3+
:doc:`Usage Examples </usage-examples>`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
This guide shows you how to create an application that uses the **Go driver**
2+
to connect to a **MongoDB Atlas cluster**. If you prefer to connect to MongoDB
3+
using a different driver or programming language, see our
4+
:driver:`list of official MongoDB drivers <>`.
5+
6+
The Go driver lets you connect to and communicate with MongoDB clusters
7+
from a Go application.
8+
9+
MongoDB Atlas is a fully-managed cloud database service that hosts your data
10+
on MongoDB clusters. In this guide, we show you how to get started with your
11+
own free (no credit card required) cluster.
12+
13+
Follow the steps below to connect your Go application with a MongoDB Atlas
14+
cluster.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
When you run ``main.go``, it should output the details of the
2+
movie from the sample dataset which will look something like this:
3+
4+
.. code-block:: json
5+
6+
{
7+
"_id": "573a1398f29313caabce9682",
8+
...
9+
"title": "Back to the Future",
10+
...
11+
}
12+
13+
If you receive no output or an error, check whether you included the proper
14+
connection string in your ``main.go`` file, and whether you loaded the
15+
sample dataset in your MongoDB Atlas cluster

source/quick-start.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,84 @@ Quick Start
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. include:: /includes/quick-start/overview.rst
14+
15+
Set up Your Project
16+
-------------------
17+
18+
Initialize with Go Mod
19+
~~~~~~~~~~~~~~~~~~~~~~
20+
21+
Create a new directory and initialize your project with ``go mod``.
22+
23+
.. code-block:: shell
24+
25+
mkdir go-quickstart
26+
cd go-quickstart
27+
go mod init go-quickstart
28+
29+
.. _add-mongodb-dependency:
30+
31+
Add MongoDB as a Dependency
32+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
34+
Use ``go get`` to add the Go driver as a dependency.
35+
36+
.. code-block:: shell
37+
38+
go get go.mongodb.org/mongo-driver/mongo
39+
40+
Create a MongoDB Cluster
41+
------------------------
42+
43+
.. include:: /includes/quick-start/atlas-setup.rst
44+
45+
46+
Query Your MongoDB Cluster from Your Application
47+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48+
49+
Next, create a file to contain your application called ``main.go``
50+
in the base directory of your project. Use the following sample
51+
code to run a query on your sample dataset in MongoDB Atlas.
52+
53+
Set the value of the ``uri`` variable with your MongoDB Atlas connection
54+
string, or create an environmental variable with the name ``MONGODB_URI``
55+
with your Atlas connection string.
56+
57+
.. code-block:: sh
58+
59+
export MONGODB_URI='<your atlas connection string>'
60+
61+
.. note::
62+
63+
Make sure to replace the "<password>" section of the connection string with
64+
the password you created for your user that has **atlasAdmin** permissions:
65+
66+
.. literalinclude:: /includes/quick-start/main.go
67+
:language: go
68+
:dedent:
69+
70+
Run the sample code with the following command from your command line:
71+
72+
.. code-block:: shell
73+
74+
go run main.go
75+
76+
77+
.. include:: /includes/quick-start/query-output.rst
78+
79+
.. tip::
80+
81+
If your output is empty, ensure you have loaded the
82+
:atlas:`sample datasets </sample-data/>` into your cluster.
83+
84+
After completing this step, you should have a working application that uses
85+
the Go driver to connect to your MongoDB cluster, run a query on the
86+
sample data, and print out the result.
87+
88+
89+
90+
Next steps
91+
----------
92+
93+
.. include:: /includes/quick-start/next-steps.rst

0 commit comments

Comments
 (0)