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.
91 changes: 61 additions & 30 deletions source/includes/quick-start/atlas-setup.rst
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
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, 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
-----------------------
~~~~~~~~~~~~~~~~~~~~~~~

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 step, 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 :guilabel:`Copy` button to copy the connection string 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
the :atlas:`Atlas driver connection guide </driver-connection>`
Expand Down
2 changes: 1 addition & 1 deletion source/includes/quick-start/query-output.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
When you run ``main.go``, it should output the details of the movie from
When you run ``main.go``, it outputs the details of the movie from
the sample dataset which looks something like the following:

.. code-block:: json
Expand Down
107 changes: 62 additions & 45 deletions source/quick-start.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,84 +25,101 @@ Set Up Your Project
Use Go Mod to Initialize a Project
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create a new directory and initialize your project by using ``go mod``.
.. procedure::
:style: connected

.. code-block:: shell
.. step:: Create and initialize your project

mkdir go-quickstart
cd go-quickstart
go mod init go-quickstart
Create a new directory and initialize your project using ``go mod``.

.. _golang-add-mongodb-dependency:
.. code-block:: shell

Add MongoDB as a Dependency
~~~~~~~~~~~~~~~~~~~~~~~~~~~
mkdir go-quickstart
cd go-quickstart
go mod init go-quickstart

Use ``go get`` to add the Go driver as a dependency.
.. step:: Add the Go driver as a dependency

.. code-block:: shell
Use ``go get`` to add the Go driver as a dependency.

go get go.mongodb.org/mongo-driver/v2/mongo
.. code-block:: shell

Add Other Dependencies
~~~~~~~~~~~~~~~~~~~~~~
go get go.mongodb.org/mongo-driver/v2/mongo

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.
.. step:: Add other dependencies

.. code-block:: shell
Use ``go get`` to add any necessary 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.

go get github.com/joho/godotenv
.. code-block:: shell

go get github.com/joho/godotenv

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

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

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

.. procedure::
:style: connected

.. step:: Create a new file

Create a new file called ``main.go`` in the base directory of your project.

.. code-block:: shell

touch main.go

.. step:: Add your connection string

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:: bash

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.
export MONGODB_URI='<your atlas connection string>'

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.
.. note::

.. code-block:: bash
Make sure to replace the "<password>" section of the connection string with
the password you created for your user that has **atlasAdmin** permissions.

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 your output is empty, 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 Go driver 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
Loading