Skip to content

DOCSP-43025 Update Quick Start #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Binary file modified source/includes/figures/atlas_connection_copy_string_go.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified source/includes/figures/atlas_connection_select_cluster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 64 additions & 33 deletions source/includes/quick-start/atlas-setup.rst
Original file line number Diff line number Diff line change
@@ -1,49 +1,80 @@
Set up a Free Tier Cluster in Atlas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After setting up your Go project dependencies, create a MongoDB cluster
where you can store and manage your data. Complete the
:atlas:`Get Started with Atlas </getting-started>` guide to set up a new
Atlas account, free tier MongoDB cluster, load datasets, and
interact with the data.
You can create a free tier MongoDB deployment on MongoDB Atlas to store and
manage your data. MongoDB Altas hosts and manages your MongoDB database in the
cloud.

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

.. step:: Create a free tier cluster

Complete the :atlas:`Get Started with Atlas </getting-started>` guide to
set up a new Atlas account, create a free tier MongoDB cluster, load
datasets, and interact with the data.

.. step:: Save your credentials

After you create your database user, save that user's username and
password to a safe location for use in an upcoming step.

After completing these steps, you have a new MongoDB cluster deployed in
Atlas, a new database user, and :atlas:`sample datasets loaded </sample-data/>`
into your cluster.

.. _golang-connect-to-your-cluster:

Connect to your Cluster
-----------------------
Connect to Your Cluster

Check failure on line 28 in source/includes/quick-start/atlas-setup.rst

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.Titles] 'Connect to Your Cluster' should be in AP title case. Raw Output: {"message": "[MongoDB.Titles] 'Connect to Your Cluster' should be in AP title case.", "location": {"path": "source/includes/quick-start/atlas-setup.rst", "range": {"start": {"line": 28, "column": 1}}}, "severity": "ERROR"}
~~~~~~~~~~~~~~~~~~~~~~~

You can connect to your MongoDB deployment by providing a
**connection URI**, also called a *connection string*, which
instructs the driver on how to connect to a MongoDB deployment
and how to behave while connected.

The connection string includes the hostname or IP address and
port of your deployment, the authentication mechanism, user credentials
when applicable, and connection options.

.. procedure::
:style: connected

.. step:: Retrieve your MongoDB Atlas connection string

To retrieve your connection string for the cluster you created in the
previous section, log in to your Atlas account. Navigate to the
:guilabel:`Database` section and click :guilabel:`Clusters`. Click the
:guilabel:`Connect` button for the cluster that you want to connect to as
shown below:

.. figure:: /includes/figures/atlas_connection_select_cluster.png
:alt: Atlas Connection GUI cluster selection screen

In this step, you create and run an application that uses the Go
driver to connect to your MongoDB cluster and run a query on the sample
data.
Then, select the :guilabel:`Drivers` option under the :guilabel:`Connect
to your application` header. Select "Go" from the :guilabel:`Driver`
selection menu and the version that best matches the version you installed
from the :guilabel:`Version` selection menu.

You pass instructions to the driver on where and how to connect to your
MongoDB cluster in a string called the *connection string*. This string
includes information on the hostname or IP address and port of your
cluster, authentication mechanism, user credentials when applicable, and
other connection options.
.. step:: Copy your connection string

To retrieve your connection string for the cluster and user you created in
the previous step, log into your Atlas account and navigate to the
:guilabel:`Database` section and click the :guilabel:`Connect` button for the cluster that you
want to connect to as shown below.
Click the button to the right of the connection string to copy it to your
clipboard, as shown in the following screenshot:

.. figure:: /includes/figures/atlas_connection_select_cluster.png
:alt: Atlas Connection GUI cluster selection screen
.. figure:: /includes/figures/atlas_connection_copy_string_go.png
:alt: Atlas Connection GUI connection string screen

Proceed to the :guilabel:`Connect Your Application` step and select the Go
driver. Then, click the :guilabel:`Copy` button to copy the *connection string*
to your clipboard as shown below.
.. step:: Update the placeholders

.. figure:: /includes/figures/atlas_connection_copy_string_go.png
:alt: Atlas Connection GUI connection string screen
Paste this connection string into a file in your preferred text editor
and replace the ``<db_username>`` and ``<db_password>`` placeholders with
your database user's username and password.

Save this file to a safe location for use in the next step.

Save your Atlas connection string in a safe location that you can access
for the next step.
After completing these steps, you have a connection string that corresponds your
Atlas cluster.

To learn more about connecting to the Go driver through Atlas, see
To learn more about connecting to the {+driver-long+} through Atlas, see
the :atlas:`Atlas driver connection guide </driver-connection>`
and select **Go** from the *Select your language* dropdown.
and select **Go** from the :guilabel:`Select your language` dropdown.
5 changes: 0 additions & 5 deletions source/includes/quick-start/main.go
Original file line number Diff line number Diff line change
@@ -7,17 +7,12 @@ import (
"log"
"os"

"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}

uri := os.Getenv("MONGODB_URI")
docs := "www.mongodb.com/docs/drivers/go/current/"
if uri == "" {
4 changes: 2 additions & 2 deletions source/includes/quick-start/next-steps.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Learn how to read and modify data using the Go driver in our Fundamentals
CRUD guide or how to perform common operations from our
Learn how to read and modify data using the {+driver-long+} in the
:ref:`golang-crud` section or how to perform common operations from our
:ref:`golang-usage-examples`.
4 changes: 2 additions & 2 deletions source/includes/quick-start/overview.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
This guide shows you how to create an application that uses the **Go driver**
This guide shows you how to create an application that uses the **{+driver-long+}**
to connect to a **MongoDB Atlas cluster**. If you prefer to connect to MongoDB
using a different driver or programming language, see our
:driver:`list of official MongoDB drivers <>`.

The Go driver lets you connect to and communicate with MongoDB clusters
The {+driver-short+} lets you connect to and communicate with MongoDB clusters
from a Go application.

MongoDB Atlas is a fully-managed cloud database service that hosts your data
10 changes: 3 additions & 7 deletions source/includes/quick-start/query-output.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
When you run ``main.go``, it should output the details of the movie from
the sample dataset which looks something like the following:
When you run ``main.go``, it outputs the details of the movie from
the sample dataset, which resembles the following:

.. code-block:: json

@@ -8,8 +8,4 @@ the sample dataset which looks something like the following:
...
"title": "Back to the Future",
...
}

If you receive no output or an error, check whether you properly set up
your environment variable and whether you loaded the sample dataset in
your MongoDB Atlas cluster.
}
110 changes: 59 additions & 51 deletions source/quick-start.txt
Original file line number Diff line number Diff line change
@@ -22,87 +22,95 @@ Go Driver Quick Start
Set Up Your Project
-------------------

Use Go Mod to Initialize a Project
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. procedure::
:style: connected

Create a new directory and initialize your project by using ``go mod``.
.. step:: Create and initialize your project

.. code-block:: shell
Create a new directory and initialize your project by using ``go mod``, as
shown in the following commands:

mkdir go-quickstart
cd go-quickstart
go mod init go-quickstart
.. code-block:: shell

.. _golang-add-mongodb-dependency:
mkdir go-quickstart
cd go-quickstart
go mod init go-quickstart

Add MongoDB as a Dependency
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. step:: Add the {+driver-long+} as a dependency

Use ``go get`` to add the Go driver as a dependency.
Use ``go get`` to add the {+driver-short+} as a dependency, as shown in
the following command:

.. code-block:: shell
.. code-block:: shell

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

Add Other Dependencies
~~~~~~~~~~~~~~~~~~~~~~

Use ``go get`` to add any additional dependencies. This Quick Start
uses the ``godotenv`` package to read a MongoDB connection string
from an environment variable to avoid embedding credentials within
source code.

.. code-block:: shell

go get github.com/joho/godotenv
go get go.mongodb.org/mongo-driver/v2/mongo

Create a MongoDB Cluster
------------------------

.. include:: /includes/quick-start/atlas-setup.rst

Query Your MongoDB Cluster from Your Application
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------------------------------------------------

.. procedure::
:style: connected

.. step:: Add your connection string

In your terminal, run the following command to create an environment
variable called ``MONGODB_URI`` and set your Atlas connection string as
its value:

.. code-block:: bash

export MONGODB_URI='<your atlas connection string>'

.. note::

Make sure to replace the ``<db_password>`` section of the connection
string with the password you created for your user that has
**atlasAdmin** permissions.

.. step:: Create a new file

Next, create a file to contain your application called ``main.go``
in the base directory of your project. Use the following sample
code to run a query on your sample dataset in MongoDB Atlas.
Run the following command from the base directory of your project to
create a new file called ``main.go``:

Specify your MongoDB Atlas connection string as the value of the
``uri`` variable, or create an environment variable called
``MONGODB_URI`` and set your Atlas connection string as its value.
.. code-block:: shell

.. code-block:: bash
touch main.go

export MONGODB_URI='<your atlas connection string>'
.. step:: Create your Go application

.. note::
Copy and paste the following code into your ``main.go`` file. This code
runs a query on your sample dataset in MongoDB Atlas.

Make sure to replace the "<password>" section of the connection string with
the password you created for your user that has **atlasAdmin** permissions.
.. literalinclude:: /includes/quick-start/main.go
:language: go
:dedent:

.. literalinclude:: /includes/quick-start/main.go
:language: go
:dedent:
.. step:: Run your application

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

.. code-block:: bash
.. code-block:: bash

go run main.go
go run main.go

.. include:: /includes/quick-start/query-output.rst
.. include:: /includes/quick-start/query-output.rst

.. tip::
.. tip::

If your output is empty, ensure you have loaded the
:atlas:`sample datasets </sample-data/>` into your cluster.
If you receive no output or an error, check whether you properly set up
your environment variable and ensure you have loaded the
:atlas:`sample datasets </sample-data/>` into your cluster.

After completing this step, you should have a working application that uses
the Go driver to connect to your MongoDB cluster, run a query on the
sample data, and print out the result.
After you complete these steps, you have a working application that uses
the {+driver-long+} to connect to your MongoDB cluster, runs a query on the
sample data, and prints out the result.

Next steps
Next Steps
----------

.. include:: /includes/quick-start/next-steps.rst