-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-47273: FAQ reorg #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,12 @@ | |||||
Connect to MongoDB | ||||||
================== | ||||||
|
||||||
.. contents:: On this page | ||||||
:local: | ||||||
:backlinks: none | ||||||
:depth: 1 | ||||||
:class: singlecol | ||||||
|
||||||
.. facet:: | ||||||
:name: genre | ||||||
:values: reference | ||||||
|
@@ -200,3 +206,118 @@ class. Select the tab that corresponds to your preferred class. | |||||
.build(); | ||||||
MongoClient mongoClient = MongoClients.create(settings); | ||||||
|
||||||
ked Questions | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
-------------------------- | ||||||
|
||||||
This section answers questions that may arise when | ||||||
connecting to MongoDB. | ||||||
|
||||||
Why are there two types of ``MongoClient`` in the Java driver? | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
There are two types of ``MongoClient`` because we wanted a cleaner API | ||||||
for new users that didn't have the confusion of including multiple CRUD | ||||||
Frequently AsAPIs. We wanted to ensure that the new CRUD API was available in a Java | ||||||
package structure that would work well with Java module support | ||||||
introduced in Java 9. | ||||||
|
||||||
Which type of ``MongoClient`` should I use? | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
New applications generally use the ``com.mongodb.client.MongoClient`` interface, | ||||||
which supports the following features: | ||||||
|
||||||
- Configuration with ``MongoClientSettings`` and ``ConnectionString``. You can create | ||||||
instances of this interface by using factory methods defined in the ``com.mongodb.client.MongoClients`` | ||||||
class. | ||||||
- Access to the CRUD API by using ``MongoDatabase``, and from there, ``MongoCollection``. | ||||||
|
||||||
Use the ``com.mongodb.MongoClient`` class if you require support for the legacy API, which supports | ||||||
the following features: | ||||||
|
||||||
- Configuration by using ``MongoClientOptions`` and ``MongoClientURI``. | ||||||
- Access to the CRUD API by using ``DB``, and from there, ``DBCollection``. You can access this API | ||||||
by using the ``getDB()`` method. | ||||||
|
||||||
For applications that require a mix of the new and legacy APIs, ``com.mongodb.MongoClient`` also supports | ||||||
the following features: | ||||||
|
||||||
- Configuration by using ``MongoClientSettings`` and ``ConnectionString``, the only difference | ||||||
being that you create instances via constructors instead of a factory class. | ||||||
- Access to the CRUD API using ``MongoDatabase``, and from there, ``MongoCollection``. You can access this | ||||||
API by using the ``getDatabase()`` method. | ||||||
|
||||||
How do I prevent the "java.lang.NoClassDefFoundError: com/mongodb/MongoClient" error? | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
You might encounter a ``java.lang.NoClassDefFoundError`` exception when your | ||||||
Java runtime environment cannot locate a class file at runtime. When you | ||||||
attempt to run application code that uses the {+driver-long+}, you must include | ||||||
the appropriate driver JAR files on the classpath. | ||||||
|
||||||
If you receive this error after adding the {+driver-short+} JAR files to | ||||||
your classpath, check the following items in your environment: | ||||||
|
||||||
- The JAR files exist in the locations specified by the classpath. | ||||||
- The classpath syntax is correct. | ||||||
- If you define the classpath in an environment variable, the Java runtime | ||||||
environment uses that variable. | ||||||
- If you use a dependency manager, it does not report any unresolvable conflicts. | ||||||
|
||||||
.. tip:: | ||||||
|
||||||
This error contains the package and class name, which can help you identify | ||||||
which driver JAR might be missing from your classpath. To locate the | ||||||
driver JAR that the error refers to, check each of the entries in the | ||||||
:ref:`API documentation <java-api-landing>`. | ||||||
|
||||||
How do I prevent the "com.mongodb.MongoSecurityException" error? | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
Your application might throw this exception if you specify invalid or | ||||||
incorrectly formatted credentials when connecting to a MongoDB deployment. | ||||||
|
||||||
If you receive this error when you attempt to connect to a MongoDB deployment, | ||||||
check the following items in your code: | ||||||
|
||||||
- The connection URI corresponds to the correct MongoDB deployment. | ||||||
To learn more about setting your connection URI, see :ref:`connection-uri`. | ||||||
|
||||||
- The credentials for the authentication mechanism that you specified are | ||||||
correct. To learn how to specify your credentials, see the | ||||||
:ref:`authentication-mechanisms` and :ref:`enterprise-authentication-mechanisms` | ||||||
guides. | ||||||
|
||||||
- The name of the authentication database that you specified is correct. To | ||||||
learn how to set up the users and roles for your MongoDB deployment, see | ||||||
`Manage Users and Roles <https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles/>`__ | ||||||
in the Server documentation. | ||||||
|
||||||
How do I prevent the "IllegalStateException: state should be: open" error? | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
You might encounter this exception if you call an operation on a ``MongoClient`` | ||||||
instance that closed its connections to MongoDB. Once the ``close()`` method | ||||||
is called on the ``MongoClient``, any further operation calls on that instance | ||||||
throw this exception. | ||||||
|
||||||
To avoid this exception, do not call operations on ``MongoClient`` instance | ||||||
after any code that calls ``close()`` on it. | ||||||
|
||||||
.. tip:: | ||||||
|
||||||
The code that closes the ``MongoClient`` instance might be difficult to | ||||||
locate in certain cases. To locate potential sources of this exception, | ||||||
search for the following cases: | ||||||
|
||||||
- Calls to ``close()`` on a ``MongoClient`` instance | ||||||
- Operation calls on a ``MongoClient`` instance that are outside the scope | ||||||
of the try-with-resources statement in which the ``MongoClient`` is | ||||||
declared | ||||||
|
||||||
If your application uses a framework to manage the ``MongoClient`` | ||||||
such as Spring Boot, check the documentation of the framework to locate the | ||||||
best practices for managing the connection behavior. | ||||||
|
||||||
To learn more about accessing MongoDB from Spring Boot, see | ||||||
`Spring Boot and MongoDB <https://www.mongodb.com/compatibility/spring-boot>`__. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -591,4 +591,4 @@ to MongoDB: | |
:end-before: end SslSettings | ||
:language: java | ||
:emphasize-lines: 3-4 | ||
:dedent: | ||
:dedent: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a whitespace thing? Not a big deal if you can't get rid of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'm not sure why this is showing up as edited |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,6 +206,48 @@ see the following API documentation: | |
- `getDefaultCodecRegistry() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html?is-external=true#getDefaultCodecRegistry()>`__ | ||
- `fromProviders() <{+api+}/apidocs/bson/org/bson/codecs/configuration/CodecRegistries.html#fromProviders(org.bson.codecs.configuration.CodecProvider...)>`__ | ||
|
||
Frequently Asked Questions | ||
-------------------------- | ||
|
||
This section answers questions that may arise when storing | ||
POJOs in MongoDB. | ||
|
||
Do I have to specify an ID field value myself? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
No, the ``PojoCodecProvider`` automatically generates an ObjectId. | ||
|
||
Can the ID field be a compound key? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Yes. For an example of this, see `our implementation <https://github.com/niccottrell/mongo-java-tests/blob/master/src/test/PojoCompoundIdTest.java>`__ | ||
in Github. | ||
|
||
Can I use polymorphism in a POJO accessor? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Yes, by using a discriminator. For more information, see the :ref:`Discriminators | ||
<pojo-discriminators>` section of the POJO Customization guide. | ||
|
||
Can I mix private, protected, and public setters and getters? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
No. The native POJO codec assumes that getters and setters have the same | ||
modifiers for each field. | ||
|
||
For example, the following methods throws an exception during encoding: | ||
|
||
.. code-block:: java | ||
|
||
private String getField(); | ||
public String setField(String x); | ||
|
||
How do I fix: "org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class X."? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This exception means you must register a codec for the class since | ||
none exist. | ||
|
||
Summary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been removing the Summary section on the pages I've touched. I can't see what value they provide. |
||
------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would set this to 2 on all the docs you touch for consistency