diff --git a/source/connect/mongoclient.txt b/source/connect/mongoclient.txt index 61a880fe..b2434e12 100644 --- a/source/connect/mongoclient.txt +++ b/source/connect/mongoclient.txt @@ -87,6 +87,30 @@ A standard connection string includes the following components: For more information about creating a connection string, see :manual:`Connection Strings ` in the MongoDB Server documentation. +.. _golang-usage-examples-env-variable: + +Environment Variable +~~~~~~~~~~~~~~~~~~~~ + +You can access your connection string as an environment variable +by using the following code: + +.. code-block:: go + + uri := os.Getenv("MONGODB_URI") + +You can use `GoDotEnv `__ to define +your environment variable. + +Add the following application configuration in your ``.env`` file at the +root of your project, replacing the placeholders with the values for your +deployment's connection string. To learn more, see the +`GoDotEnv documentation `__. + +.. code-block:: + + MONGODB_URI=mongodb+srv://:@?retryWrites=true&w=majority + Create a Client to Connect to MongoDB Atlas ------------------------------------------- diff --git a/source/includes/fundamentals/code-snippets/CRUD/changeStream.go b/source/includes/fundamentals/code-snippets/CRUD/changeStream.go index d8a4043f..8a333a93 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/changeStream.go +++ b/source/includes/fundamentals/code-snippets/CRUD/changeStream.go @@ -23,7 +23,7 @@ type Course struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go b/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go index a2fe6cca..2b334e5d 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go +++ b/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go @@ -23,7 +23,7 @@ type Course struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/delete.go b/source/includes/fundamentals/code-snippets/CRUD/delete.go index 482793d7..803310ce 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/delete.go +++ b/source/includes/fundamentals/code-snippets/CRUD/delete.go @@ -24,7 +24,7 @@ type Book struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/limit.go b/source/includes/fundamentals/code-snippets/CRUD/limit.go index fd0a078d..6fad89fb 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/limit.go +++ b/source/includes/fundamentals/code-snippets/CRUD/limit.go @@ -23,7 +23,7 @@ type Course struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go index 1001e735..f83c7ebf 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/retrieve.go +++ b/source/includes/fundamentals/code-snippets/CRUD/retrieve.go @@ -25,7 +25,7 @@ type Tea struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go index 29baf8e6..95c6827e 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go +++ b/source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go @@ -12,7 +12,7 @@ import ( func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } // start-retryable-example diff --git a/source/includes/fundamentals/code-snippets/CRUD/runCommand.go b/source/includes/fundamentals/code-snippets/CRUD/runCommand.go index 87e7b2f4..6dcdc41d 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/runCommand.go +++ b/source/includes/fundamentals/code-snippets/CRUD/runCommand.go @@ -17,7 +17,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/skip.go b/source/includes/fundamentals/code-snippets/CRUD/skip.go index 42c8b4ac..0760598c 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/skip.go +++ b/source/includes/fundamentals/code-snippets/CRUD/skip.go @@ -24,7 +24,7 @@ type Course struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/sort.go b/source/includes/fundamentals/code-snippets/CRUD/sort.go index a56808cb..733ef1d7 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/sort.go +++ b/source/includes/fundamentals/code-snippets/CRUD/sort.go @@ -24,7 +24,7 @@ type Course struct { func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/fundamentals/code-snippets/logging.go b/source/includes/fundamentals/code-snippets/logging.go index 8d1a1be6..4b252850 100644 --- a/source/includes/fundamentals/code-snippets/logging.go +++ b/source/includes/fundamentals/code-snippets/logging.go @@ -24,7 +24,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } //standardLogging(uri) //customLogging(uri) diff --git a/source/includes/usage-examples/code-snippets/bulk.go b/source/includes/usage-examples/code-snippets/bulk.go index 21a67a7e..3ae26c9f 100644 --- a/source/includes/usage-examples/code-snippets/bulk.go +++ b/source/includes/usage-examples/code-snippets/bulk.go @@ -32,7 +32,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/command.go b/source/includes/usage-examples/code-snippets/command.go index 67f0bd0e..85803a86 100644 --- a/source/includes/usage-examples/code-snippets/command.go +++ b/source/includes/usage-examples/code-snippets/command.go @@ -21,7 +21,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/count.go b/source/includes/usage-examples/code-snippets/count.go index 068d862b..b2a81fdf 100644 --- a/source/includes/usage-examples/code-snippets/count.go +++ b/source/includes/usage-examples/code-snippets/count.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/deleteMany.go b/source/includes/usage-examples/code-snippets/deleteMany.go index b208e54f..0bcc60a3 100644 --- a/source/includes/usage-examples/code-snippets/deleteMany.go +++ b/source/includes/usage-examples/code-snippets/deleteMany.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/deleteOne.go b/source/includes/usage-examples/code-snippets/deleteOne.go index 9362c7cb..8d23b987 100644 --- a/source/includes/usage-examples/code-snippets/deleteOne.go +++ b/source/includes/usage-examples/code-snippets/deleteOne.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/distinct.go b/source/includes/usage-examples/code-snippets/distinct.go index b8a18bbb..f3f0a6af 100644 --- a/source/includes/usage-examples/code-snippets/distinct.go +++ b/source/includes/usage-examples/code-snippets/distinct.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/find.go b/source/includes/usage-examples/code-snippets/find.go index af0bf4e3..a70573c8 100644 --- a/source/includes/usage-examples/code-snippets/find.go +++ b/source/includes/usage-examples/code-snippets/find.go @@ -34,7 +34,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/findOne.go b/source/includes/usage-examples/code-snippets/findOne.go index 31f66530..e3ba4447 100644 --- a/source/includes/usage-examples/code-snippets/findOne.go +++ b/source/includes/usage-examples/code-snippets/findOne.go @@ -34,7 +34,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/insertMany.go b/source/includes/usage-examples/code-snippets/insertMany.go index 13750c3f..577dab73 100644 --- a/source/includes/usage-examples/code-snippets/insertMany.go +++ b/source/includes/usage-examples/code-snippets/insertMany.go @@ -31,7 +31,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/insertOne.go b/source/includes/usage-examples/code-snippets/insertOne.go index fc2b5c7d..41493f85 100644 --- a/source/includes/usage-examples/code-snippets/insertOne.go +++ b/source/includes/usage-examples/code-snippets/insertOne.go @@ -31,7 +31,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/replace.go b/source/includes/usage-examples/code-snippets/replace.go index d2e1aeb9..84328efe 100644 --- a/source/includes/usage-examples/code-snippets/replace.go +++ b/source/includes/usage-examples/code-snippets/replace.go @@ -32,7 +32,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/struct-tag.go b/source/includes/usage-examples/code-snippets/struct-tag.go index ea6f5bd8..b5d55dd1 100644 --- a/source/includes/usage-examples/code-snippets/struct-tag.go +++ b/source/includes/usage-examples/code-snippets/struct-tag.go @@ -34,7 +34,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/updateMany.go b/source/includes/usage-examples/code-snippets/updateMany.go index 7e8578a7..fdf4d235 100644 --- a/source/includes/usage-examples/code-snippets/updateMany.go +++ b/source/includes/usage-examples/code-snippets/updateMany.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/updateOne.go b/source/includes/usage-examples/code-snippets/updateOne.go index 825cd1a6..5e84e294 100644 --- a/source/includes/usage-examples/code-snippets/updateOne.go +++ b/source/includes/usage-examples/code-snippets/updateOne.go @@ -20,7 +20,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) diff --git a/source/includes/usage-examples/code-snippets/watch.go b/source/includes/usage-examples/code-snippets/watch.go index 67d4725a..9fa2ec03 100644 --- a/source/includes/usage-examples/code-snippets/watch.go +++ b/source/includes/usage-examples/code-snippets/watch.go @@ -21,7 +21,7 @@ func main() { var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable.") + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri))