|
4 | 4 |
|
5 | 5 | .. default-domain:: mongodb
|
6 | 6 |
|
7 |
| -Frequently Asked Questions |
8 |
| - |
9 | 7 | .. contents:: On this page
|
10 | 8 | :local:
|
11 | 9 | :backlinks: none
|
12 |
| - :depth: 1 |
| 10 | + :depth: 2 |
13 | 11 | :class: singlecol
|
14 | 12 |
|
15 |
| -Lorem ipsum. |
| 13 | +General |
| 14 | +------- |
| 15 | + |
| 16 | +Why are there two types of ``MongoClient`` in the Java driver? |
| 17 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 18 | + |
| 19 | +There are two types of ``MongoClient`` because we wanted a cleaner API |
| 20 | +for new users that didn't have the confusion of including multiple CRUD |
| 21 | +API's. We wanted to ensure that the new CRUD API was available in a Java |
| 22 | +package structure that would work well with Java module support |
| 23 | +introduced in Java 9. |
| 24 | + |
| 25 | +Which type of ``MongoClient`` should I use? |
| 26 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 27 | + |
| 28 | +New applications should generally use the |
| 29 | +``com.mongodb.client.MongoClient`` interface, which supports: |
| 30 | + |
| 31 | +- Configuration with ``MongoClientSettings`` and ``ConnectionString``. You can create instances of this interface via factory methods defined in the ``com.mongodb.client.MongoClients`` class. |
| 32 | +- CRUD API using ``MongoDatabase``, and from there, ``MongoCollection`` |
| 33 | + |
| 34 | +You should use ``com.mongodb.MongoClient`` class if you require support for the legacy API, which supports: |
| 35 | + |
| 36 | +- Configuration with ``MongoClientOptions`` and ``MongoClientURI`` |
| 37 | +- CRUD API using ``DB``, and from there, ``DBCollection``. You can access this API via the ``getDB()`` method. |
| 38 | + |
| 39 | +For applications that require a mix of the new and legacy APIs, ``com.mongodb.MongoClient`` also supports: |
| 40 | + |
| 41 | +- Configuration with ``MongoClientSettings`` and ``ConnectionString``, the only difference being that you create instances via constructors instead of a factory class. |
| 42 | +- CRUD API using ``MongoDatabase``, and from there, ``MongoCollection``. You can access this API via the ``getDatabase()`` method. |
| 43 | + |
| 44 | +POJOs |
| 45 | +----- |
| 46 | + |
| 47 | +Do I have to specify an ID field value myself? |
| 48 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 49 | + |
| 50 | +Imagine you have the following entity: |
| 51 | + |
| 52 | +.. code-block:: java |
| 53 | + |
| 54 | + public class Entity { |
| 55 | + public final ObjectId id; |
| 56 | + public String value; |
| 57 | + |
| 58 | + public Entity(){ } |
| 59 | + } |
| 60 | + |
| 61 | +Can the ID field be a compound key? |
| 62 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +Yes. For an example of this, see `our implementation <https://github.com/niccottrell/mongo-java-tests/blob/master/src/test/PojoCompoundIdTest.java>`_ |
| 65 | + |
| 66 | +Can I use polymorphism in a POJO accessor? |
| 67 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +Yes, by using a discriminator. |
| 70 | + |
| 71 | +What is the discriminator? |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +The discriminator is for cases where you want to use inheritance, and |
| 75 | +store multiple types of documents within the same collection or parent |
| 76 | +document (in case you embed sub-documents). |
| 77 | + |
| 78 | +For example, if you have an ``Event`` class, that you extend in Java (e.g. |
| 79 | +``MachineEvent`` or ``NetworkEvent``), using the discriminator identifies |
| 80 | +which class the PojoCodec should use to serialize/deserialize the |
| 81 | +document. |
| 82 | + |
| 83 | +Can I control serialization of ``LocalDate``? |
| 84 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 | + |
| 86 | +Yes, the 3.7 Java driver adds native support for ``JSR-310 Instant``, |
| 87 | +``LocalDate`` & ``LocalDateTime``. |
| 88 | + |
| 89 | +Can I serialize a ``java.util.Date`` as a string in format **yyyy-mm-dd**? |
| 90 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 | + |
| 92 | +Yes, you can build your own codec for this class and add it to the registry. |
| 93 | + |
| 94 | +Add the codec to the top your registry before the ``DefaultCodecRegister`` |
| 95 | +and before the ``PojoCodecProvider``: |
| 96 | + |
| 97 | +.. code-block:: java |
| 98 | + |
| 99 | + static final CodecRegistry CODEC_REGISTRY = fromRegistries( |
| 100 | + CodecRegistries.fromCodecs( |
| 101 | + new MyDateAsStringCodec()), |
| 102 | + MongoClient.getDefaultCodecRegistry(), |
| 103 | + fromProviders(pojoCodecProvider)); |
| 104 | + |
| 105 | +Can I make POJOs read/write directly to the field and not use the getters/setters at all? |
| 106 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 107 | + |
| 108 | +You can configure the PojoCodecProvider to use the |
| 109 | +``SET_PRIVATE_FIELDS_CONVENTION``, which sets a private field through |
| 110 | +reflection if no public setter is available. |
| 111 | + |
| 112 | +Can I mix private, protected and public setters and getters? |
| 113 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 114 | + |
| 115 | +No. The native POJO codec assumes that getters/setters have the same |
| 116 | +modifiers for each field. |
| 117 | + |
| 118 | +For example, the following methods throws an exception during encoding: |
| 119 | + |
| 120 | +.. code-block:: java |
| 121 | + |
| 122 | + private String getField(); |
| 123 | + public String setField(String x); |
| 124 | + |
| 125 | +How do I fix: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class X .? |
| 126 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 127 | + |
| 128 | +This exception means you need to register a codec for the class since |
| 129 | +there is none at the moment. |
| 130 | + |
| 131 | +How do I specify the collection name for a particular POJO class? Is there an annotation? |
| 132 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 133 | + |
| 134 | +There is no annotation. We recommend adding a static in your class as shown: |
| 135 | + |
| 136 | +.. code-block:: java |
| 137 | + |
| 138 | + public class Person { |
| 139 | + public static final String COLLECTION_NAME = "people"; |
| 140 | + } |
| 141 | + |
| 142 | +The following snippet specifies the collection name for a particular |
| 143 | +POJO class: |
| 144 | + |
| 145 | +.. code-block:: java |
| 146 | + |
| 147 | + database.getCollection(Person.COLLECTION_NAME, Person.class); |
| 148 | + |
| 149 | +If you are unable to find the answer to your question here, try our forums and |
| 150 | +support channels listed in the :doc:`Issues and Help <issues-and-help>` |
| 151 | +section. |
0 commit comments