@@ -10,8 +10,9 @@ Quick Start (POJO Examples)
10
10
:depth: 1
11
11
:class: singlecol
12
12
13
- This guide follows from the :ref:`Quick Start guide <javars-quickstart>`, but uses Plain
14
- Old Java Objects, or POJOs, to model documents.
13
+ This guide follows from the :ref:`Quick Start guide
14
+ <javars-quickstart>` but uses Plain Old Java Objects, or POJOs, to
15
+ model documents.
15
16
16
17
The code examples in this guide come from the `PojoQuickTour.java
17
18
<{+driver-source-gh+}/blob/master/driver-reactive-streams/src/examples/reactivestreams/tour/PojoQuickTour.java>`__
@@ -54,101 +55,23 @@ this guide:
54
55
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
55
56
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
56
57
57
- - POJO class definitions. The full code is available on GitHub
58
- for the ``Person`` and ``Address`` POJOs. The following code
59
- demonstrates the main implementation details:
60
-
61
- .. code-block:: java
62
-
63
- public final class Person {
64
- private ObjectId id;
65
- private String name;
66
- private int age;
67
- private Address address;
68
-
69
- public Person() {
70
- }
71
-
72
- public ObjectId getId() {
73
- return id;
74
- }
75
-
76
- public void setId(final ObjectId id) {
77
- this.id = id;
78
- }
79
-
80
- public String getName() {
81
- return name;
82
- }
83
-
84
- public void setName(final String name) {
85
- this.name = name;
86
- }
87
-
88
- public int getAge() {
89
- return age;
90
- }
91
-
92
- public void setAge(final int age) {
93
- this.age = age;
94
- }
95
-
96
- public Address getAddress() {
97
- return address;
98
- }
99
-
100
- public void setAddress(final Address address) {
101
- this.address = address;
102
- }
103
-
104
- // Rest of implementation
105
- }
106
-
107
- public final class Address {
108
- private String street;
109
- private String city;
110
- private String zip;
111
-
112
- public Address() {
113
- }
114
-
115
- public String getStreet() {
116
- return street;
117
- }
118
-
119
- public void setStreet(final String street) {
120
- this.street = street;
121
- }
122
-
123
- public String getCity() {
124
- return city;
125
- }
126
-
127
- public void setCity(final String city) {
128
- this.city = city;
129
- }
130
-
131
- public String getZip() {
132
- return zip;
133
- }
134
-
135
- public void setZip(final String zip) {
136
- this.zip = zip;
137
- }
138
-
139
- // Rest of implementation
140
- }
58
+ - POJO class definitions. Copy the full code for the ``Person`` and
59
+ ``Address`` POJOs from the driver source repository on
60
+ GitHub:
61
+
62
+ - `Person class <{+driver-source-gh+}/blob/master/driver-reactive-streams/src/examples/reactivestreams/tour/Person.java>`__
63
+ - `Address class <{+driver-source-gh+}/blob/master/driver-reactive-streams/src/examples/reactivestreams/tour/Address.java>`__
141
64
142
65
Creating a Custom CodecRegistry
143
66
-------------------------------
144
67
145
68
Before you can use a POJO with the driver, you need to configure the
146
- ``CodecRegistry`` to include a codec to handle the translation to and
69
+ ``CodecRegistry`` to include a codec that handles the translation to and
147
70
from BSON for your POJOs. The simplest way to do that is to use
148
71
the ``PojoCodecProvider.builder()`` method to create and configure a
149
72
``CodecProvider``.
150
73
151
- The following example combines the default codec registry, with
74
+ The following example combines the default codec registry with
152
75
the ``PojoCodecProvider`` configured to automatically create POJO
153
76
``Codec`` instances:
154
77
@@ -174,24 +97,24 @@ use:
174
97
175
98
- Set it when instantiating a ``MongoClient`` object:
176
99
177
- .. code-block:: java
178
-
179
- MongoClientSettings settings = MongoClientSettings.builder()
180
- .codecRegistry(pojoCodecRegistry)
181
- .build();
182
- MongoClient mongoClient = MongoClients.create(settings);
100
+ .. code-block:: java
101
+
102
+ MongoClientSettings settings = MongoClientSettings.builder()
103
+ .codecRegistry(pojoCodecRegistry)
104
+ .build();
105
+ MongoClient mongoClient = MongoClients.create(settings);
183
106
184
107
- Use an alternative ``CodecRegistry`` with a ``MongoDatabase``:
185
108
186
- .. code-block:: java
187
-
188
- database = database.withCodecRegistry(pojoCodecRegistry);
109
+ .. code-block:: java
110
+
111
+ database = database.withCodecRegistry(pojoCodecRegistry);
189
112
190
113
- Use an alternative ``CodecRegistry`` with a ``MongoCollection``:
191
114
192
- .. code-block:: java
193
-
194
- collection = collection.withCodecRegistry(pojoCodecRegistry);
115
+ .. code-block:: java
116
+
117
+ collection = collection.withCodecRegistry(pojoCodecRegistry);
195
118
196
119
Inserting a POJO into MongoDB
197
120
-----------------------------
@@ -210,6 +133,8 @@ Before you can insert a POJO into MongoDB, create a
210
133
Insert a Person Instance
211
134
~~~~~~~~~~~~~~~~~~~~~~~~
212
135
136
+ .. _javars-pojo-insertone:
137
+
213
138
To insert a ``Person`` into the collection, use the collection's
214
139
``insertOne()`` method:
215
140
@@ -222,7 +147,8 @@ Insert Multiple Person Instances
222
147
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223
148
224
149
To insert multiple ``Person`` instances, you can use the collection's
225
- ``insertMany()`` method, which takes a list of ``Person`` instances.
150
+ ``insertMany()`` method, which takes a list of ``Person`` instances as a
151
+ parameter.
226
152
227
153
The following example will add multiple ``Person`` instances into the
228
154
collection:
@@ -244,18 +170,22 @@ To query the collection, you can use the ``find()`` method.
244
170
The following example prints all the ``Person`` instances in the
245
171
collection:
246
172
247
- .. code-block:: java
173
+ .. io-code-block::
174
+ :copyable: true
248
175
249
- collection.find().subscribe(new PrintToStringSubscriber<>());
176
+ .. input::
177
+ :language: java
250
178
251
- The example prints the following result:
179
+ collection.find().subscribe(new PrintToStringSubscriber<>());
252
180
253
- .. code-block:: none
181
+ .. output::
182
+ :language: none
183
+ :visible: false
254
184
255
- Person{id='...', name='Ada Byron', age=20, address=Address{street='St James Square', city='London', zip='W1'}}
256
- Person{id='...', name='Charles Babbage', age=45, address=Address{street='5 Devonshire Street', city='London', zip='W11'}}
257
- Person{id='...', name='Alan Turing', age=28, address=Address{street='Bletchley Hall', city='Bletchley Park', zip='MK12'}}
258
- Person{id='...', name='Timothy Berners-Lee', age=61, address=Address{street='Colehill', city='Wimborne', zip='null'}}
185
+ Person{id='...', name='Ada Byron', age=20, address=Address{street='St James Square', city='London', zip='W1'}}
186
+ Person{id='...', name='Charles Babbage', age=45, address=Address{street='5 Devonshire Street', city='London', zip='W11'}}
187
+ Person{id='...', name='Alan Turing', age=28, address=Address{street='Bletchley Hall', city='Bletchley Park', zip='MK12'}}
188
+ Person{id='...', name='Timothy Berners-Lee', age=61, address=Address{street='Colehill', city='Wimborne', zip='null'}}
259
189
260
190
Specify a Query Filter
261
191
----------------------
@@ -266,28 +196,32 @@ filter objects, the driver provides ``Filters`` helper methods.
266
196
267
197
.. important::
268
198
269
- When querying POJOs you *must* query against the document field name
199
+ When querying POJOs, you *must* query against the document field name
270
200
and not the POJO property name. By default, they are the same but it
271
201
is possible to change how POJO property names are mapped.
272
202
273
203
Get A Single Person That Matches a Filter
274
204
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275
205
276
- For example, to find the first ``Person`` in the database that lives in
277
- `` Wimborne``, pass an ``eq()`` filter object to specify the equality
278
- condition:
206
+ The following example finds the first ``Person`` in the database that
207
+ has an ``address.city`` value of `` Wimborne`` by passing an ``eq()``
208
+ filter object to specify the equality condition:
279
209
280
- .. code-block:: java
210
+ .. io-code-block::
211
+ :copyable: true
281
212
282
- collection.find(eq("address.city", "Wimborne"))
283
- .first()
284
- .subscribe(new PrintToStringSubscriber<>());
213
+ .. input::
214
+ :language: java
285
215
286
- The example prints one document:
216
+ collection.find(eq("address.city", "Wimborne"))
217
+ .first()
218
+ .subscribe(new PrintToStringSubscriber<>());
287
219
288
- .. code-block:: none
220
+ .. output::
221
+ :language: none
222
+ :visible: false
289
223
290
- Person{id='591dbc2550852fa685b3ad1a', name='Timothy Berners-Lee', age=61, address=Address{street='Colehill', city='Wimborne', zip='null'}}
224
+ Person{id='591dbc2550852fa685b3ad1a', name='Timothy Berners-Lee', age=61, address=Address{street='Colehill', city='Wimborne', zip='null'}}
291
225
292
226
Get All Person Instances That Match a Filter
293
227
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -305,12 +239,12 @@ Update Documents
305
239
To update documents in a collection, you can use the collection's
306
240
``updateOne()`` and ``updateMany()`` methods.
307
241
308
- Pass the following components to the methods:
242
+ Pass the following parameters to the methods:
309
243
310
244
- Filter object to determine the document or documents to update. To
311
245
specify an empty filter and match all ``Person`` instances, use an
312
246
empty ``Document`` object.
313
- - An update document that specifies the modifications. To view a list of
247
+ - Update document that specifies the modifications. To view a list of
314
248
the available operators, see :manual:`Update Operators
315
249
</reference/operator/update/>` in the Server manual.
316
250
@@ -350,11 +284,12 @@ that have a ``zip`` value:
350
284
Replace a Single Person
351
285
~~~~~~~~~~~~~~~~~~~~~~~
352
286
353
- Another method to change an existing ``Person`` instance is to use
287
+ Another way to change an existing ``Person`` instance is to use
354
288
the ``replaceOne()`` method.
355
289
356
290
The following example replaces a ``Person`` named ``"Ada Lovelace"``
357
- with the original ``Person`` instance:
291
+ with the ``Person`` instance referenced by the ``ada`` variable in the
292
+ :ref:`preceding insertOne example <javars-pojo-insertone>`:
358
293
359
294
.. code-block:: java
360
295
@@ -378,10 +313,10 @@ Delete a Single Person That Matches a Filter
378
313
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
379
314
380
315
To delete a single ``Person`` that matches a filter, use the
381
- ``deleteOne()`` method:
316
+ ``deleteOne()`` method.
382
317
383
- The following example deletes one ``Person`` who lives in
384
- the city ``Wimborne``:
318
+ The following example deletes one ``Person`` that has an
319
+ ``address. city`` value of ``Wimborne``:
385
320
386
321
.. code-block:: java
387
322
@@ -394,8 +329,8 @@ Delete All Person Instances That Match a Filter
394
329
To delete multiple ``Person`` instances that match a filter, use the
395
330
``deleteMany()`` method.
396
331
397
- The following example deletes all ``Person`` instances that live in
398
- the city ``London``:
332
+ The following example deletes all ``Person`` instances that have an
333
+ ``address. city`` value of ``London``:
399
334
400
335
.. code-block:: java
401
336
0 commit comments