Skip to content

Commit d0e0c8b

Browse files
authored
DOCSP-47273: FAQ reorg (#644)
* DOCSP-47273: FAQ reorg * edits * pojo * RM feedback
1 parent 17f01d8 commit d0e0c8b

File tree

8 files changed

+367
-490
lines changed

8 files changed

+367
-490
lines changed

source/connection/connect.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
Connect to MongoDB
55
==================
66

7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
713
.. facet::
814
:name: genre
915
:values: reference
@@ -200,3 +206,118 @@ class. Select the tab that corresponds to your preferred class.
200206
.build();
201207
MongoClient mongoClient = MongoClients.create(settings);
202208

209+
Frequently Asked Questions
210+
--------------------------
211+
212+
This section answers questions that may arise when
213+
connecting to MongoDB.
214+
215+
Why are there two types of ``MongoClient`` in the Java driver?
216+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217+
218+
There are two types of ``MongoClient`` because we wanted a cleaner API
219+
for new users that didn't have the confusion of including multiple CRUD
220+
Frequently AsAPIs. We wanted to ensure that the new CRUD API was available in a Java
221+
package structure that would work well with Java module support
222+
introduced in Java 9.
223+
224+
Which type of ``MongoClient`` should I use?
225+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226+
227+
New applications generally use the ``com.mongodb.client.MongoClient`` interface,
228+
which supports the following features:
229+
230+
- Configuration with ``MongoClientSettings`` and ``ConnectionString``. You can create
231+
instances of this interface by using factory methods defined in the ``com.mongodb.client.MongoClients``
232+
class.
233+
- Access to the CRUD API by using ``MongoDatabase``, and from there, ``MongoCollection``.
234+
235+
Use the ``com.mongodb.MongoClient`` class if you require support for the legacy API, which supports
236+
the following features:
237+
238+
- Configuration by using ``MongoClientOptions`` and ``MongoClientURI``.
239+
- Access to the CRUD API by using ``DB``, and from there, ``DBCollection``. You can access this API
240+
by using the ``getDB()`` method.
241+
242+
For applications that require a mix of the new and legacy APIs, ``com.mongodb.MongoClient`` also supports
243+
the following features:
244+
245+
- Configuration by using ``MongoClientSettings`` and ``ConnectionString``, the only difference
246+
being that you create instances via constructors instead of a factory class.
247+
- Access to the CRUD API using ``MongoDatabase``, and from there, ``MongoCollection``. You can access this
248+
API by using the ``getDatabase()`` method.
249+
250+
How do I prevent the "java.lang.NoClassDefFoundError: com/mongodb/MongoClient" error?
251+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
252+
253+
You might encounter a ``java.lang.NoClassDefFoundError`` exception when your
254+
Java runtime environment cannot locate a class file at runtime. When you
255+
attempt to run application code that uses the {+driver-long+}, you must include
256+
the appropriate driver JAR files on the classpath.
257+
258+
If you receive this error after adding the {+driver-short+} JAR files to
259+
your classpath, check the following items in your environment:
260+
261+
- The JAR files exist in the locations specified by the classpath.
262+
- The classpath syntax is correct.
263+
- If you define the classpath in an environment variable, the Java runtime
264+
environment uses that variable.
265+
- If you use a dependency manager, it does not report any unresolvable conflicts.
266+
267+
.. tip::
268+
269+
This error contains the package and class name, which can help you identify
270+
which driver JAR might be missing from your classpath. To locate the
271+
driver JAR that the error refers to, check each of the entries in the
272+
:ref:`API documentation <java-api-landing>`.
273+
274+
How do I prevent the "com.mongodb.MongoSecurityException" error?
275+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
276+
277+
Your application might throw this exception if you specify invalid or
278+
incorrectly formatted credentials when connecting to a MongoDB deployment.
279+
280+
If you receive this error when you attempt to connect to a MongoDB deployment,
281+
check the following items in your code:
282+
283+
- The connection URI corresponds to the correct MongoDB deployment.
284+
To learn more about setting your connection URI, see :ref:`connection-uri`.
285+
286+
- The credentials for the authentication mechanism that you specified are
287+
correct. To learn how to specify your credentials, see the
288+
:ref:`authentication-mechanisms` and :ref:`enterprise-authentication-mechanisms`
289+
guides.
290+
291+
- The name of the authentication database that you specified is correct. To
292+
learn how to set up the users and roles for your MongoDB deployment, see
293+
`Manage Users and Roles <https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles/>`__
294+
in the Server documentation.
295+
296+
How do I prevent the "IllegalStateException: state should be: open" error?
297+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298+
299+
You might encounter this exception if you call an operation on a ``MongoClient``
300+
instance that closed its connections to MongoDB. Once the ``close()`` method
301+
is called on the ``MongoClient``, any further operation calls on that instance
302+
throw this exception.
303+
304+
To avoid this exception, do not call operations on ``MongoClient`` instance
305+
after any code that calls ``close()`` on it.
306+
307+
.. tip::
308+
309+
The code that closes the ``MongoClient`` instance might be difficult to
310+
locate in certain cases. To locate potential sources of this exception,
311+
search for the following cases:
312+
313+
- Calls to ``close()`` on a ``MongoClient`` instance
314+
- Operation calls on a ``MongoClient`` instance that are outside the scope
315+
of the try-with-resources statement in which the ``MongoClient`` is
316+
declared
317+
318+
If your application uses a framework to manage the ``MongoClient``
319+
such as Spring Boot, check the documentation of the framework to locate the
320+
best practices for managing the connection behavior.
321+
322+
To learn more about accessing MongoDB from Spring Boot, see
323+
`Spring Boot and MongoDB <https://www.mongodb.com/compatibility/spring-boot>`__.

source/connection/mongoclientsettings.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,4 +591,4 @@ to MongoDB:
591591
:end-before: end SslSettings
592592
:language: java
593593
:emphasize-lines: 3-4
594-
:dedent:
594+
:dedent:

source/data-formats/document-data-format-bson.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,52 @@ for that tool:
8989
If you are not using one of the preceding tools, you can include it in
9090
your project by downloading the JAR file directly from the
9191
`sonatype repository <https://repo1.maven.org/maven2/org/mongodb/bson/>`__.
92+
93+
Frequently Asked Questions
94+
--------------------------
95+
96+
This section answers questions that may arise when using the BSON data format.
97+
98+
How do I prevent the "IllegalArgumentException: Invalid BSON field name" error?
99+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100+
101+
Your application might throw this exception if you pass an incorrectly formatted
102+
document to an operation and you are using {+driver-short+} v4.7 or earlier.
103+
104+
.. note::
105+
106+
In driver versions v4.8 and later, this error message was replaced by one
107+
that includes more specific details on what was incorrectly formatted.
108+
109+
For example, the driver throws this error when you call an update operation
110+
and incorrectly omit the update operator as shown in the following code
111+
example:
112+
113+
.. code-block:: java
114+
:emphasize-lines: 4
115+
:copyable: false
116+
117+
// incorrectly formatted update document
118+
collection.updateOne(
119+
new Document().append("name", "fizz"),
120+
new Document().append("name", "buzz")
121+
);
122+
123+
To avoid this error, use the builder class for the appropriate operation.
124+
The driver offers builder classes to create syntactically correct BSON for
125+
MongoDB operations. The preceding example can be correctly expressed using
126+
the builder classes, as shown in the following code example:
127+
128+
.. code-block:: java
129+
:emphasize-lines: 7
130+
:copyable: false
131+
132+
// Builder class imports
133+
import static com.mongodb.client.model.Filters.*;
134+
import static com.mongodb.client.model.Updates.*;
135+
136+
// ...
137+
138+
collection.updateOne(eq("name", "fizz"), set("name", "buzz"));
139+
140+
To learn more about the available builders classes, see the :ref:`<java-builders>` guide.

source/data-formats/document-data-format-pojo.txt

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,44 @@ see the following API documentation:
206206
- `getDefaultCodecRegistry() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html?is-external=true#getDefaultCodecRegistry()>`__
207207
- `fromProviders() <{+api+}/apidocs/bson/org/bson/codecs/configuration/CodecRegistries.html#fromProviders(org.bson.codecs.configuration.CodecProvider...)>`__
208208

209-
Summary
210-
-------
211-
212-
This guide describes how to convert data between BSON and the POJO format
213-
by performing the following tasks:
214-
215-
- Instantiate a ``PojoCodecProvider`` which contains codecs that define how to
216-
encode and decode data between BSON and the POJO fields.
217-
- Specify the **automatic** conversion behavior for the ``PojoCodecProvider``
218-
to apply the ``Codecs`` to any class and its properties.
219-
- Add the ``PojoCodecProvider`` to a ``CodecRegistry``, and specify the
220-
registry on an instance of a ``MongoDatabase`` or ``MongoCollection``.
221-
- Perform CRUD operations that use the sample POJO class.
209+
Frequently Asked Questions
210+
--------------------------
211+
212+
This section answers questions that may arise when storing
213+
POJOs in MongoDB.
214+
215+
Do I have to specify an ID field value myself?
216+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217+
218+
No, the ``PojoCodecProvider`` automatically generates an ObjectId.
219+
220+
Can the ID field be a compound key?
221+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222+
223+
Yes. For an example of this, see `our implementation <https://github.com/niccottrell/mongo-java-tests/blob/master/src/test/PojoCompoundIdTest.java>`__
224+
in Github.
225+
226+
Can I use polymorphism in a POJO accessor?
227+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228+
229+
Yes, by using a discriminator. For more information, see the :ref:`Discriminators
230+
<pojo-discriminators>` section of the POJO Customization guide.
231+
232+
Can I mix private, protected, and public setters and getters?
233+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234+
235+
No. The native POJO codec assumes that getters and setters have the same
236+
modifiers for each field.
237+
238+
For example, the following methods throws an exception during encoding:
239+
240+
.. code-block:: java
241+
242+
private String getField();
243+
public String setField(String x);
244+
245+
How do I fix: "org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class X."?
246+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247+
248+
This exception means you must register a codec for the class since
249+
none exist.

source/data-formats/pojo-customization.txt

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,4 +759,57 @@ a codec for ``enum`` types if you need one, such as the one in the default
759759
codec registry.
760760

761761
See the documentation on the :ref:`default codec registry <codecs-default-codec-registry>`
762-
For more information about how to register the codecs it includes.
762+
for more information about how to register the codecs it includes.
763+
764+
Frequently Asked Questions
765+
--------------------------
766+
767+
This section answers questions that may arise when defining
768+
data conversion logic.
769+
770+
Can I control serialization of ``LocalDate``?
771+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
772+
773+
Yes, the {+driver-short+} v3.7 adds native support for ``JSR-310 Instant``,
774+
``LocalDate`` and ``LocalDateTime``.
775+
776+
Can I serialize a ``java.util.Date`` as a string in format **yyyy-mm-dd**?
777+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
778+
779+
Yes, you can build your own codec for this class and add it to the registry.
780+
781+
Add the codec to the first in the list of providers, before the default codec
782+
registry and before the ``PojoCodecProvider``:
783+
784+
.. literalinclude:: /includes/faq/code-snippets/FaqExample.java
785+
:language: java
786+
:emphasize-lines: 3
787+
:dedent:
788+
:start-after: start myDateAsStringCodec
789+
:end-before: end myDateAsStringCodec
790+
791+
Can I make POJOs read/write directly to the field and not use the getters/setters at all?
792+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
793+
794+
You can configure the ``PojoCodecProvider`` to use the
795+
``SET_PRIVATE_FIELDS_CONVENTION``, which sets a private field through
796+
reflection if no public setter is available.
797+
798+
How do I specify the collection name for a particular POJO class? Is there an annotation?
799+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
800+
801+
There is no annotation. We recommend adding a static string in your class as shown
802+
in the following code:
803+
804+
.. code-block:: java
805+
806+
public class Person {
807+
public static final String COLLECTION_NAME = "people";
808+
}
809+
810+
The following snippet specifies the collection name for a particular
811+
POJO class:
812+
813+
.. code-block:: java
814+
815+
database.getCollection(Person.COLLECTION_NAME, Person.class);

0 commit comments

Comments
 (0)