|
4 | 4 | Connect to MongoDB
|
5 | 5 | ==================
|
6 | 6 |
|
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
7 | 13 | .. facet::
|
8 | 14 | :name: genre
|
9 | 15 | :values: reference
|
@@ -200,3 +206,118 @@ class. Select the tab that corresponds to your preferred class.
|
200 | 206 | .build();
|
201 | 207 | MongoClient mongoClient = MongoClients.create(settings);
|
202 | 208 |
|
| 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>`__. |
0 commit comments