@@ -4,6 +4,12 @@ Usage Examples
4
4
5
5
.. default-domain:: mongodb
6
6
7
+ .. contents:: On this page
8
+ :local:
9
+ :backlinks: none
10
+ :depth: 1
11
+ :class: singlecol
12
+
7
13
.. toctree::
8
14
9
15
/usage-examples/find-operations
@@ -23,47 +29,67 @@ Overview
23
29
--------
24
30
25
31
Usage 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:
30
33
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
36
36
37
37
How to Use the Usage Examples
38
38
-----------------------------
39
39
40
40
These examples use the :atlas:`sample datasets <sample-data>` provided by
41
41
Atlas. 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
45
44
:guides:`import the sample dataset into a local MongoDB instance
46
45
</server/import/>`.
47
46
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
49
48
example into your development environment of choice. You can follow the
50
49
: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>`.
54
55
55
56
.. 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>`__.
56
71
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
+ ~~~~~~~~~~~~~~~~~
59
84
60
85
You 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