@@ -4,6 +4,12 @@ Usage Examples
44
55.. default-domain:: mongodb
66
7+ .. contents:: On this page
8+ :local:
9+ :backlinks: none
10+ :depth: 1
11+ :class: singlecol
12+
713.. toctree::
814
915 /usage-examples/find-operations
@@ -23,47 +29,67 @@ Overview
2329--------
2430
2531Usage examples provide convenient starting points for popular MongoDB
26- operations. Each example provides:
27-
28- - an explanation of the operation in the example showing the
29- purpose and a sample use case for the method
32+ operations. Each example provides the following:
3033
31- - an explanation of how to use the operation, including parameters,
32- return values, and common exceptions you might encounter
33-
34- - a full Go program that you can copy and paste to run the example
35- in your own environment
34+ - A full Go program that you can run in your own environment
35+ - The expected result after running the program
3636
3737How to Use the Usage Examples
3838-----------------------------
3939
4040These examples use the :atlas:`sample datasets <sample-data>` provided by
4141Atlas. You can load them into your database on the free tier of MongoDB
42- Atlas by following the
43- :atlas:`Get Started with Atlas Guide <getting-started/#atlas-getting-started>`
44- or you can
42+ Atlas by following the :atlas:`Get Started with Atlas Guide
43+ <getting-started/#atlas-getting-started>` or you can
4544:guides:`import the sample dataset into a local MongoDB instance
4645</server/import/>`.
4746
48- Once you have imported the dataset, you can copy and paste a usage
47+ Once you import the dataset, you can copy and paste a usage
4948example into your development environment of choice. You can follow the
5049:doc:`quick start guide </quick-start>` to learn more about getting
51- started with the MongoDB Go Driver. Once you've copied a usage example,
52- you'll need to edit the connection string to get the example connected to
53- your instance of MongoDB:
50+ started with the MongoDB Go Driver.
51+
52+ To connect the example to your MongoDB instance, you must :ref:`define
53+ an environment variable <usage-examples-env-variable>` with
54+ :ref:`your connection string <usage-examples-connection-string>`.
5455
5556.. code-block:: go
57+
58+ uri := os.Getenv("MONGODB_URI")
59+
60+ .. _usage-examples-env-variable:
61+
62+ Environment Variable
63+ ~~~~~~~~~~~~~~~~~~~~
64+
65+ You can use `GoDotEnv <https://github.com/joho/godotenv>`__ to define
66+ your environment variable.
67+
68+ Add the following application configuration in your ``.env`` file at the
69+ root of your project. For more information, see the
70+ `GoDotEnv documentation <https://github.com/joho/godotenv#usage>`__.
5671
57- // Replace the following with your MongoDB deployment's connection string.
58- uri := "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
72+ .. code-block::
73+
74+ MONGODB_URI=mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority
75+
76+ .. note::
77+
78+ Replace the preceding connection string with :ref:`your MongoDB deployment's connection string <usage-examples-connection-string>`.
79+
80+ .. _usage-examples-connection-string:
81+
82+ Connection String
83+ ~~~~~~~~~~~~~~~~~
5984
6085You can use the :guides:`Atlas Connectivity Guide </cloud/connectionstring/>`
61- to enable connectivity to your instance of Atlas and find the
62- :manual:`connection string </reference/connection-string/>` to replace the
63- ``uri`` variable in the usage example. If your instance uses
64- :manual:`SCRAM authentication </core/security-scram/>`, you can replace
65- ``<user>`` with your username, ``<password>`` with your password, and
66- ``<cluster-url>`` with the IP address or URL of your instance.
67-
68- For more information about connecting to your MongoDB instance, see the
69- Connection Guide.
86+ to enable connectivity to your Atlas instance and find the
87+ :manual:`connection string </reference/connection-string/>` to define your
88+ ``MONGODB_URI`` environment variable to run our usage examples. If your
89+ instance uses :manual:`SCRAM authentication </core/security-scram/>`,
90+ you can replace ``<user>`` with your username, ``<password>`` with your
91+ password, and ``<cluster-url>`` with the IP address or URL of your instance.
92+
93+ For more information about connecting to your MongoDB instance, see our
94+ <TODO: Connection Guide>.
95+
0 commit comments