Skip to content

Commit 1124133

Browse files
authored
DOCSP-47019 Shift Go ToC (#499)
* DOCSP-47019 Shift Go ToC * generate staging * SA review * connection options drawer * fi crud refs
1 parent 7ad3564 commit 1124133

File tree

102 files changed

+1848
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1848
-89
lines changed

snooty.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name = "golang"
22
title = "Go Driver"
33
toc_landing_pages = [
4-
"/fundamentals/connections",
5-
"/fundamentals/crud",
6-
"/usage-examples",
4+
"/connect",
5+
"/crud",
6+
"/monitoring-and-logging",
7+
"/security"
78
]
89

910
intersphinx = [
File renamed without changes.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
.. _golang-faq:
2+
3+
===
4+
FAQ
5+
===
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, connection error, question, help
13+
:description: Find answers to common questions about the MongoDB Go Driver, including connection pooling, error handling, and BSON to JSON conversion.
14+
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 1
19+
:class: singlecol
20+
21+
This page contains frequently asked questions and their corresponding answers.
22+
23+
.. tip::
24+
25+
If you can't find an answer to your problem on this page,
26+
see the :ref:`golang-issues-and-help` page for next steps and more
27+
resources.
28+
29+
Why Am I Getting Errors While Connecting to MongoDB?
30+
----------------------------------------------------
31+
32+
If you have trouble connecting to a MongoDB deployment, see
33+
the :ref:`Connection Troubleshooting Guide <golang-connection-troubleshooting>`
34+
for possible solutions.
35+
36+
.. _golang-faq-connection-pool:
37+
38+
How Does Connection Pooling Work in the {+driver-short+}?
39+
---------------------------------------------------------
40+
41+
Every ``Client`` instance has a built-in connection pool for each server
42+
in your MongoDB topology. Connection pools open sockets on demand to support
43+
concurrent MongoDB operations, or `goroutines
44+
<https://www.golang-book.com/books/intro/10>`__, in your application.
45+
46+
The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
47+
defaults to ``100``. If the number of in-use connections to a server reaches
48+
the value of ``maxPoolSize``, the next request to that server will wait
49+
until a connection becomes available.
50+
51+
The ``Client`` instance opens two additional sockets per server in your
52+
MongoDB topology for monitoring the server's state.
53+
54+
For example, a client connected to a 3-node replica set opens 6
55+
monitoring sockets. It also opens the necessary sockets to support
56+
an application's concurrent operations on each server, up to
57+
the value of ``maxPoolSize``. If ``maxPoolSize`` is ``100`` and the
58+
application only uses the primary (the default), then only the primary
59+
connection pool grows and there can be at most ``106`` total connections. If the
60+
application uses a :ref:`read preference <golang-read-pref>` to query the
61+
secondary nodes, their pools also grow and there can be ``306`` total connections.
62+
63+
Additionally, connection pools are rate-limited such that each connection pool
64+
can only create, at maximum, the value of ``maxConnecting`` connections
65+
in parallel at any time. Any additional goroutine stops waiting in the
66+
following cases:
67+
68+
- One of the existing goroutines finishes creating a connection, or
69+
an existing connection is checked back into the pool.
70+
- The driver's ability to reuse existing connections improves due to
71+
rate-limits on connection creation.
72+
73+
You can set the minimum number of concurrent connections to
74+
each server by using the ``minPoolSize`` option, which defaults to ``0``.
75+
After setting ``minPoolSize``, the connection pool is initialized with
76+
this number of sockets. If sockets close due to any network errors, causing
77+
the total number of sockets (both in use and idle) to drop below the minimum, more sockets
78+
open until the minimum is reached.
79+
80+
You can set the maximum number of milliseconds that a connection can
81+
remain idle in the pool before being removed and replaced with
82+
the ``maxIdleTimeMS`` option, which defaults to ``None`` (no limit).
83+
84+
The following default configuration for a ``Client`` works for most applications:
85+
86+
.. code-block:: go
87+
88+
client, err := mongo.Connect(options.Client().ApplyURI("<connection string>"))
89+
90+
Create a client once for each process, and reuse it for all
91+
operations. It is a common mistake to create a new client for each
92+
request, which is very inefficient.
93+
94+
To support high numbers of concurrent MongoDB operations
95+
within one process, you can increase ``maxPoolSize``. Once the pool
96+
reaches its maximum size, additional operations wait for sockets
97+
to become available.
98+
99+
The driver does not limit the number of operations that
100+
can wait for sockets to become available and it is the application's
101+
responsibility to limit the size of its pool to bound queuing
102+
during a load spike. Operations can wait for any length of time
103+
unless you define the ``waitQueueTimeoutMS`` option.
104+
105+
An operation that waits more than the length of time defined by
106+
``waitQueueTimeoutMS`` for a socket raises a connection error. Use this
107+
option if it is more important to bound the duration of operations
108+
during a load spike than it is to complete every operation.
109+
110+
When ``Client.Disconnect()`` is called by any goroutine, the driver
111+
closes all idle sockets and closes all sockets that are in
112+
use as they are returned to the pool.
113+
114+
How Can I Fix the "WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel" Error?
115+
--------------------------------------------------------------------------------------------------------------------------
116+
117+
The ``bson.Marshal()`` method requires a parameter that can be decoded
118+
into a BSON document, such as the ``bson.D`` type. This error occurs
119+
when you pass something *other* than a BSON document to
120+
``bson.Marshal()``.
121+
122+
The ``WriteNull`` error occurs when you pass a ``null`` to
123+
``bson.Marshal()``. Situations in which a similar error can occur
124+
include the following:
125+
126+
- You pass a string to ``bson.Marshal()``, causing a ``WriteString`` error.
127+
- You pass a boolean to ``bson.Marshal()``, causing a ``WriteBoolean`` error.
128+
- You pass an integer to ``bson.Marshal()``, causing a ``WriteInt32`` error.
129+
130+
You may encounter this error when you perform a CRUD operation that
131+
internally uses the ``bson.Marshal()`` method or when you call
132+
``bson.Marshal()`` directly to encode data.
133+
134+
The following code produces a ``WriteNull`` error because the driver
135+
cannot encode the ``null`` value of ``sortOrder`` to BSON during
136+
the ``FindOneAndUpdate()`` operation:
137+
138+
.. code-block:: go
139+
140+
var sortOrder bson.D
141+
opts := options.FindOneAndUpdate().SetSort(sortOrder)
142+
143+
updateDocument := bson.D{{"$inc", bson.D{{"counter", 1}}}}
144+
result := coll.FindOneAndUpdate(context.TODO(), bson.D{}, updateDocument, opts)
145+
if err := result.Err(); err != nil {
146+
panic(err)
147+
}
148+
149+
The following code shows how to correctly initialize the ``sortOrder``
150+
variable as a ``bson.D`` type so that the driver can convert it to BSON:
151+
152+
.. code-block:: go
153+
154+
sortOrder := bson.D{}
155+
156+
How Do I Convert a BSON Document to JSON?
157+
-----------------------------------------
158+
159+
The driver provides a variety of marshaler methods that can be used to
160+
convert a BSON document to JSON, such as the ``MarshalExtJSON()``
161+
method. To view a readable form of the JSON encoding, you must use
162+
an unmarshaler method or string type-casting to parse the JSON byte
163+
format.
164+
165+
The following code converts a BSON document to JSON using the
166+
``MarshalExtJSON()`` method, then parses and prints the JSON byte array
167+
using string type-casting:
168+
169+
.. io-code-block::
170+
:copyable: true
171+
172+
.. input::
173+
:language: go
174+
:emphasize-lines: 3
175+
176+
bsonDocument := bson.D{{"hello", "world"}}
177+
178+
jsonBytes, err := bson.MarshalExtJSON(bsonDocument, true, false)
179+
if err != nil {
180+
panic(err)
181+
}
182+
183+
fmt.Println(string(jsonBytes))
184+
185+
.. output::
186+
:language: none
187+
:visible: false
188+
189+
{"hello":"world"}
190+
191+
To learn more about conversions between BSON and Go types, see the
192+
:ref:`golang-bson` guide.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. _golang-issues-and-help:
2+
3+
=============
4+
Issues & Help
5+
=============
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: suggestion, github
13+
:description: Find support for the MongoDB Go Driver through community forums, report bugs or feature requests via JIRA, and create pull requests to contribute.
14+
15+
We are lucky to have a vibrant MongoDB Go community that includes users
16+
with varying levels of experience using the Go driver. We find the quickest
17+
way to get support for general questions is through the `MongoDB Community Forums <https://community.mongodb.com>`_.
18+
19+
To learn more, refer to our `support channels <http://www.mongodb.org/about/support>`_.
20+
21+
Bugs / Feature Requests
22+
-----------------------
23+
24+
If you think you've found a bug or want to see a new feature in the Go
25+
driver, please open a case in our issue management tool, JIRA:
26+
27+
* `Create an account and log in <https://jira.mongodb.org>`_.
28+
* Navigate to `the GODRIVER project <https://jira.mongodb.org/browse/GODRIVER>`_.
29+
* Click **Create Issue**. Please provide as much information as possible
30+
about the issue and the steps to reproduce it.
31+
32+
Bug reports in JIRA for the Go driver and the Core Server (i.e. SERVER) project are **public**.
33+
34+
If you've identified a security vulnerability in a driver or any other
35+
MongoDB project, please report it according to the instructions found in the
36+
:manual:`Create a Vulnerability Report page </tutorial/create-a-vulnerability-report>`.
37+
38+
Pull Requests
39+
-------------
40+
41+
We are happy to accept contributions to help improve the driver. We will guide
42+
user contributions to ensure they meet the standards of the codebase. Please
43+
ensure that any pull requests include documentation, tests, and pass the
44+
**gradle** checks.
45+
46+
To get started, check out the source and work on a branch:
47+
48+
.. code-block:: bash
49+
50+
$ git clone https://github.com/mongodb/mongo-go-driver.git
51+
$ cd mongo-go-driver
52+
$ git checkout -b myNewFeature
53+
54+
Finally, follow the `Testing/Development guidelines
55+
<https://github.com/mongodb/mongo-go-driver#testing--development>`__ to
56+
ensure your code passes any newly added and existing tests.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
.. _golang-quickstart:
2+
3+
=====================
4+
Go Driver Quick Start
5+
=====================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: tutorial
16+
17+
.. meta::
18+
:keywords: tutorial, get started, code example
19+
:description: Learn to connect a Go application to a MongoDB Atlas cluster using the MongoDB Go Driver, including setting up a project, creating a cluster, and running queries.
20+
21+
.. include:: /includes/quick-start/overview.rst
22+
23+
Set Up Your Project
24+
-------------------
25+
26+
.. procedure::
27+
:style: connected
28+
29+
.. step:: Create and initialize your project
30+
31+
Create a new directory and initialize your project by using ``go mod``, as
32+
shown in the following commands:
33+
34+
.. code-block:: shell
35+
36+
mkdir go-quickstart
37+
cd go-quickstart
38+
go mod init go-quickstart
39+
40+
.. step:: Add the {+driver-long+} as a dependency
41+
42+
Use ``go get`` to add the {+driver-short+} as a dependency, as shown in
43+
the following command:
44+
45+
.. code-block:: shell
46+
47+
go get go.mongodb.org/mongo-driver/v2/mongo
48+
49+
Create a MongoDB Cluster
50+
------------------------
51+
52+
.. include:: /includes/quick-start/atlas-setup.rst
53+
54+
Query Your MongoDB Cluster from Your Application
55+
------------------------------------------------
56+
57+
.. procedure::
58+
:style: connected
59+
60+
.. step:: Add your connection string
61+
62+
In your terminal, run the following command to create an environment
63+
variable called ``MONGODB_URI`` and set your Atlas connection string as
64+
its value:
65+
66+
.. code-block:: bash
67+
68+
export MONGODB_URI='<your atlas connection string>'
69+
70+
.. note::
71+
72+
Make sure to replace the ``<db_password>`` section of the connection
73+
string with the password you created for your user that has
74+
**atlasAdmin** permissions.
75+
76+
.. step:: Create a new file
77+
78+
Run the following command from the base directory of your project to
79+
create a new file called ``main.go``:
80+
81+
.. code-block:: shell
82+
83+
touch main.go
84+
85+
.. step:: Create your Go application
86+
87+
Copy and paste the following code into your ``main.go`` file. This code
88+
runs a query on your sample dataset in MongoDB Atlas.
89+
90+
.. literalinclude:: /includes/quick-start/main.go
91+
:language: go
92+
:dedent:
93+
94+
.. step:: Run your application
95+
96+
Run the sample code with the following command from your command line:
97+
98+
.. code-block:: bash
99+
100+
go run main.go
101+
102+
.. include:: /includes/quick-start/query-output.rst
103+
104+
.. tip::
105+
106+
If you receive no output or an error, check whether you properly set up
107+
your environment variable and ensure you have loaded the
108+
:atlas:`sample datasets </sample-data/>` into your cluster.
109+
110+
After you complete these steps, you have a working application that uses
111+
the {+driver-long+} to connect to your MongoDB cluster, runs a query on the
112+
sample data, and prints out the result.
113+
114+
Next Steps
115+
----------
116+
117+
.. include:: /includes/quick-start/next-steps.rst

0 commit comments

Comments
 (0)