Skip to content

Commit 2aa1742

Browse files
committed
DOCSP-47273: FAQ reorg
1 parent 52989b6 commit 2aa1742

File tree

7 files changed

+347
-477
lines changed

7 files changed

+347
-477
lines changed

source/connection/connect.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,111 @@ class. Select the tab that corresponds to your preferred class.
200200
.build();
201201
MongoClient mongoClient = MongoClients.create(settings);
202202

203+
Frequently Asked Questions
204+
--------------------------
205+
206+
This section answers questions that may arise when
207+
connecting to MongoDB.
208+
209+
Why are there two types of ``MongoClient`` in the Java driver?
210+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211+
212+
There are two types of ``MongoClient`` because we wanted a cleaner API
213+
for new users that didn't have the confusion of including multiple CRUD
214+
APIs. We wanted to ensure that the new CRUD API was available in a Java
215+
package structure that would work well with Java module support
216+
introduced in Java 9.
217+
218+
Which type of ``MongoClient`` should I use?
219+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220+
221+
New applications generally use the
222+
``com.mongodb.client.MongoClient`` interface, which supports:
223+
224+
- Configuration with ``MongoClientSettings`` and ``ConnectionString``. You can create instances of this interface via factory methods defined in the ``com.mongodb.client.MongoClients`` class.
225+
- CRUD API using ``MongoDatabase``, and from there, ``MongoCollection``
226+
227+
Use the ``com.mongodb.MongoClient`` class if you require support for the legacy API, which supports:
228+
229+
- Configuration with ``MongoClientOptions`` and ``MongoClientURI``
230+
- CRUD API using ``DB``, and from there, ``DBCollection``. You can access this API via the ``getDB()`` method.
231+
232+
For applications that require a mix of the new and legacy APIs, ``com.mongodb.MongoClient`` also supports:
233+
234+
- Configuration with ``MongoClientSettings`` and ``ConnectionString``, the only difference being that you create instances via constructors instead of a factory class.
235+
- CRUD API using ``MongoDatabase``, and from there, ``MongoCollection``. You can access this API via the ``getDatabase()`` method.
236+
237+
How do I prevent the "java.lang.NoClassDefFoundError: com/mongodb/MongoClient" error?
238+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239+
240+
You might encounter a ``java.lang.NoClassDefFoundError`` exception when your
241+
Java runtime environment cannot locate a class file at runtime. When you
242+
attempt to run application code that uses the {+driver-long+}, you must include
243+
the appropriate driver JAR files on the classpath.
244+
245+
If you receive this error after adding the {+driver-short+} JAR files to
246+
your classpath, check the following items in your environment:
247+
248+
- The JAR files exist in the locations specified by the classpath.
249+
- The classpath syntax is correct.
250+
- If you define the classpath in an environment variable, the Java runtime
251+
environment uses that variable.
252+
- If you use a dependency manager, it does not report any unresolvable conflicts.
253+
254+
.. tip::
255+
256+
This error contains the package and class name, which can help you identify
257+
which driver JAR might be missing from your classpath. To locate the
258+
driver JAR that the error refers to, check each of the entries in the
259+
:ref:`API documentation <java-api-landing>`.
260+
261+
How do I prevent the "com.mongodb.MongoSecurityException" error?
262+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263+
264+
Your application might throw this exception if you specify invalid or
265+
incorrectly formatted credentials when connecting to a MongoDB deployment.
266+
267+
If you receive this error when you attempt to connect to a MongoDB deployment,
268+
check the following items in your code:
269+
270+
- The connection URI corresponds to the correct MongoDB deployment.
271+
To learn more about setting your connection URI, see :ref:`connection-uri`.
272+
273+
- The credentials for the authentication mechanism that you specified are
274+
correct. To learn how to specify your credentials, see the
275+
:ref:`authentication-mechanisms` and :ref:`enterprise-authentication-mechanisms`
276+
guides.
277+
278+
- The name of the authentication database that you specified is correct. To
279+
learn how to set up the users and roles for your MongoDB deployment, see
280+
`Manage Users and Roles <https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles/>`__
281+
in the Server documentation.
282+
283+
How do I prevent the "IllegalStateException: state should be: open" error?
284+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285+
286+
You might encounter this exception if you call an operation on a ``MongoClient``
287+
instance that closed its connections to MongoDB. Once the ``close()`` method
288+
is called on the ``MongoClient``, any further operation calls on that instance
289+
throw this exception.
290+
291+
To avoid this exception, do not call operations on ``MongoClient`` instance
292+
after any code that calls ``close()`` on it.
293+
294+
.. tip::
295+
296+
The code that closes the ``MongoClient`` instance might be difficult to
297+
locate in certain cases. To locate potential sources of this exception,
298+
search for the following cases:
299+
300+
- Calls to ``close()`` on a ``MongoClient`` instance
301+
- Operation calls on a ``MongoClient`` instance that are outside the scope
302+
of the try-with-resources statement in which the ``MongoClient`` is
303+
declared
304+
305+
If your application uses a framework to manage the ``MongoClient``
306+
such as Spring Boot, check the documentation of the framework to locate the
307+
best practices for managing the connection behavior.
308+
309+
To learn more about accessing MongoDB from Spring Boot, see
310+
`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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,53 @@ 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 about
97+
the BSON data format.
98+
99+
How do I prevent the "IllegalArgumentException: Invalid BSON field name" error?
100+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101+
102+
Your application might throw this exception if you pass an incorrectly formatted
103+
document to an operation and you are using a driver version v4.7 or earlier.
104+
105+
.. note::
106+
107+
In driver versions v4.8 and later, this error message was replaced by one
108+
that includes more specific details on what was incorrectly formatted.
109+
110+
For example, the driver throws this error when you call an update operation
111+
and incorrectly omit the update operator as shown in the following code
112+
example:
113+
114+
.. code-block:: java
115+
:emphasize-lines: 4
116+
:copyable: false
117+
118+
// incorrectly formatted update document
119+
collection.updateOne(
120+
new Document().append("name", "fizz"),
121+
new Document().append("name", "buzz")
122+
);
123+
124+
To avoid this error, use the builder class for the appropriate operation.
125+
The driver offers builder classes to create syntactically correct BSON for
126+
MongoDB operations. The prior example can be correctly expressed using
127+
the builder classes as shown in the following code example:
128+
129+
.. code-block:: java
130+
:emphasize-lines: 7
131+
:copyable: false
132+
133+
// Builder class imports
134+
import static com.mongodb.client.model.Filters.*;
135+
import static com.mongodb.client.model.Updates.*;
136+
137+
// ...
138+
139+
collection.updateOne(eq("name", "fizz"), set("name", "buzz"));
140+
141+
To learn more about the available builders classes, see the :ref:`<java-builders>` documentation.

source/data-formats/pojo-customization.txt

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ see the following API Documentation:
749749

750750
For more information about generics and type parameters, see the
751751
`Java language guide on Invoking and Instantiating a Generic Type <https://docs.oracle.com/javase/tutorial/java/generics/types.html#instantiation>`__.
752-
752+
753753
Enum Type Support
754754
^^^^^^^^^^^^^^^^^
755755

@@ -760,3 +760,90 @@ codec registry.
760760

761761
See the documentation on the :ref:`default codec registry <codecs-default-codec-registry>`
762762
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+
Do I have to specify an ID field value myself?
771+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
772+
773+
No, the ``PojoCodecProvider`` automatically generates an ObjectId.
774+
775+
Can the ID field be a compound key?
776+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
777+
778+
Yes. For an example of this, see `our implementation <https://github.com/niccottrell/mongo-java-tests/blob/master/src/test/PojoCompoundIdTest.java>`_
779+
780+
Can I use polymorphism in a POJO accessor?
781+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
782+
783+
Yes, by using a discriminator. For more information, see the :ref:`Discriminators
784+
<pojo-discriminators>` section.
785+
786+
Can I control serialization of ``LocalDate``?
787+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788+
789+
Yes, the 3.7 Java driver adds native support for ``JSR-310 Instant``,
790+
``LocalDate`` & ``LocalDateTime``.
791+
792+
Can I serialize a ``java.util.Date`` as a string in format **yyyy-mm-dd**?
793+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
794+
795+
Yes, you can build your own codec for this class and add it to the registry.
796+
797+
Add the codec to the first in the list of providers, before the default codec
798+
registry and before the ``PojoCodecProvider``:
799+
800+
.. literalinclude:: /includes/faq/code-snippets/FaqExample.java
801+
:language: java
802+
:emphasize-lines: 3
803+
:dedent:
804+
:start-after: start myDateAsStringCodec
805+
:end-before: end myDateAsStringCodec
806+
807+
Can I make POJOs read/write directly to the field and not use the getters/setters at all?
808+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
809+
810+
You can configure the ``PojoCodecProvider`` to use the
811+
``SET_PRIVATE_FIELDS_CONVENTION``, which sets a private field through
812+
reflection if no public setter is available.
813+
814+
Can I mix private, protected, and public setters and getters?
815+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
816+
817+
No. The native POJO codec assumes that getters/setters have the same
818+
modifiers for each field.
819+
820+
For example, the following methods throws an exception during encoding:
821+
822+
.. code-block:: java
823+
824+
private String getField();
825+
public String setField(String x);
826+
827+
How do I fix: "org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class X."?
828+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
829+
830+
This exception means you must register a codec for the class since
831+
there is none at the moment.
832+
833+
How do I specify the collection name for a particular POJO class? Is there an annotation?
834+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
835+
836+
There is no annotation. We recommend adding a static string in your class as shown:
837+
838+
.. code-block:: java
839+
840+
public class Person {
841+
public static final String COLLECTION_NAME = "people";
842+
}
843+
844+
The following snippet specifies the collection name for a particular
845+
POJO class:
846+
847+
.. code-block:: java
848+
849+
database.getCollection(Person.COLLECTION_NAME, Person.class);

0 commit comments

Comments
 (0)