Skip to content

Commit 61b2f4c

Browse files
authored
DOCSP-51826 Connection Str as Env Var (#536)
* DOCSP-51826 Connection Str as Env Var * edit urls and change section titleto be shorter for url reasons * DOCSP-51826 Check release note refs * Revert "DOCSP-51826 Check release note refs" This reverts commit 89d27e6.
1 parent 48babd3 commit 61b2f4c

26 files changed

+49
-25
lines changed

source/connect/mongoclient.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ A standard connection string includes the following components:
8787
For more information about creating a connection string, see :manual:`Connection
8888
Strings </reference/connection-string>` in the MongoDB Server documentation.
8989

90+
.. _golang-usage-examples-env-variable:
91+
92+
Environment Variable
93+
~~~~~~~~~~~~~~~~~~~~
94+
95+
You can access your connection string as an environment variable
96+
by using the following code:
97+
98+
.. code-block:: go
99+
100+
uri := os.Getenv("MONGODB_URI")
101+
102+
You can use `GoDotEnv <https://github.com/joho/godotenv>`__ to define
103+
your environment variable.
104+
105+
Add the following application configuration in your ``.env`` file at the
106+
root of your project, replacing the placeholders with the values for your
107+
deployment's connection string. To learn more, see the
108+
`GoDotEnv documentation <https://github.com/joho/godotenv#usage>`__.
109+
110+
.. code-block::
111+
112+
MONGODB_URI=mongodb+srv://<db_username>:<db_password>@<cluster-url>?retryWrites=true&w=majority
113+
90114
Create a Client to Connect to MongoDB Atlas
91115
-------------------------------------------
92116

source/includes/fundamentals/code-snippets/CRUD/changeStream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Course struct {
2323
func main() {
2424
var uri string
2525
if uri = os.Getenv("MONGODB_URI"); uri == "" {
26-
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")
26+
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")
2727
}
2828

2929
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Course struct {
2323
func main() {
2424
var uri string
2525
if uri = os.Getenv("MONGODB_URI"); uri == "" {
26-
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")
26+
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")
2727
}
2828

2929
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Book struct {
2424
func main() {
2525
var uri string
2626
if uri = os.Getenv("MONGODB_URI"); uri == "" {
27-
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")
27+
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")
2828
}
2929

3030
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/limit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Course struct {
2323
func main() {
2424
var uri string
2525
if uri = os.Getenv("MONGODB_URI"); uri == "" {
26-
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")
26+
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")
2727
}
2828

2929
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/retrieve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Tea struct {
2525
func main() {
2626
var uri string
2727
if uri = os.Getenv("MONGODB_URI"); uri == "" {
28-
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")
28+
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")
2929
}
3030

3131
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/retryableReadsWrites.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func main() {
1313
var uri string
1414
if uri = os.Getenv("MONGODB_URI"); uri == "" {
15-
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")
15+
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")
1616
}
1717

1818
// start-retryable-example

source/includes/fundamentals/code-snippets/CRUD/runCommand.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717

1818
var uri string
1919
if uri = os.Getenv("MONGODB_URI"); uri == "" {
20-
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")
20+
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")
2121
}
2222

2323
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/skip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Course struct {
2424
func main() {
2525
var uri string
2626
if uri = os.Getenv("MONGODB_URI"); uri == "" {
27-
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")
27+
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")
2828
}
2929

3030
client, err := mongo.Connect(options.Client().ApplyURI(uri))

source/includes/fundamentals/code-snippets/CRUD/sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Course struct {
2424
func main() {
2525
var uri string
2626
if uri = os.Getenv("MONGODB_URI"); uri == "" {
27-
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")
27+
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")
2828
}
2929

3030
client, err := mongo.Connect(options.Client().ApplyURI(uri))

0 commit comments

Comments
 (0)