forked from immutables/immutables.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.html
484 lines (412 loc) · 37.6 KB
/
mongo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"-->
<link rel="stylesheet" href="/gfx/bootstrap.min.css">
<link rel="stylesheet" href="/gfx/main.css">
<link rel="stylesheet" href="/gfx/code.css">
<title>MongoDB repositories</title>
</head>
<body class="page">
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-PMJSKV"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PMJSKV');</script>
<!-- End Google Tag Manager -->
<header>
<div class="container">
<a href="/">Immutables</a> ←
<h1>MongoDB repositories <a class="github-button" href="https://github.com/immutables/immutables" data-style="mega" data-count-href="/immutables/immutables/stargazers" data-count-api="/repos/immutables/immutables#stargazers_count" data-count-aria-label="# stargazers on GitHub" aria-label="Star immutables/immutables on GitHub">stars</a></h1>
</div>
</header>
<aside id="toc"></aside>
<section class="documentation">
<h2>Overview</h2>
<p>There are already a lot of tools to access MongoDB collections <a href="http://docs.mongodb.org/ecosystem/drivers/java/">using Java</a>.
Each driver or wrapper has it's own distinct features and advantages. The focus of <em>Immutables</em> repository generation
is to provide the best possible API that matches well for storing documents expressed as immutable objects.</p>
<ul>
<li>Expressive type safe API
<ul>
<li>Document field names are expressed as method names, not strings. Works nicely with auto-completion in IDE.</li>
<li>Operations and types should match.</li>
</ul></li>
<li>Asynchronous operations returning <code>Future</code>
<ul>
<li>Compose async operations.</li>
<li>IO is still synchronous underneath with dedicated thread pool.</li>
</ul></li>
</ul>
<p>One of the side goals of this module was to demonstrate that Java DSLs and APIs could be actually a lot less ugly than they usually are.</p>
<p>Generated repositories wrap the infrastructure of the official Java driver, but there are couple of places where operations are handled more efficiently in <em>Immutables</em>.
Repositories employ BSON marshaling which uses the same infrastructure for <a href="/json.html">JSON marshaling</a> using the excellent <a href="https://github.com/michel-kraemer/bson4jackson">bson4jackson</a> data-format adapter.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="c1">// Define repository for collection "items".</span>
<span class="nd">@Value.Immutable</span>
<span class="nd">@Mongo.Repository</span><span class="o">(</span><span class="s">"items"</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Item</span> <span class="o">{</span>
<span class="nd">@Mongo.Id</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">long</span> <span class="nf">id</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">String</span> <span class="nf">name</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">Set</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="nf">values</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">Optional</span><span class="o"><</span><span class="n">String</span><span class="o">></span> <span class="nf">comment</span><span class="o">();</span>
<span class="o">}</span>
<span class="c1">// Instantiate generated repository</span>
<span class="n">ItemRepository</span> <span class="n">items</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ItemRepository</span><span class="o">(</span>
<span class="n">RepositorySetup</span><span class="o">.</span><span class="na">forUri</span><span class="o">(</span><span class="s">"mongodb://localhost/test"</span><span class="o">));</span>
<span class="c1">// Create item</span>
<span class="n">Item</span> <span class="n">item</span> <span class="o">=</span> <span class="n">ImmutableItem</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"one"</span><span class="o">)</span>
<span class="o">.</span><span class="na">addValues</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="c1">// Insert async</span>
<span class="n">items</span><span class="o">.</span><span class="na">insert</span><span class="o">(</span><span class="n">item</span><span class="o">);</span> <span class="c1">// returns future</span>
<span class="n">Optional</span><span class="o"><</span><span class="n">Item</span><span class="o">></span> <span class="n">modifiedItem</span> <span class="o">=</span> <span class="n">items</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">andModifyFirst</span><span class="o">()</span> <span class="c1">// findAndModify</span>
<span class="o">.</span><span class="na">addValues</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span> <span class="c1">// $addToSet</span>
<span class="o">.</span><span class="na">setComment</span><span class="o">(</span><span class="s">"present"</span><span class="o">)</span> <span class="c1">// $set</span>
<span class="o">.</span><span class="na">returningNew</span><span class="o">()</span>
<span class="o">.</span><span class="na">update</span><span class="o">()</span> <span class="c1">// returns future</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
<span class="c1">// Update all matching documents</span>
<span class="n">items</span><span class="o">.</span><span class="na">update</span><span class="o">(</span>
<span class="n">ItemRepository</span><span class="o">.</span><span class="na">criteria</span><span class="o">()</span>
<span class="o">.</span><span class="na">idIn</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="o">)</span>
<span class="o">.</span><span class="na">nameNot</span><span class="o">(</span><span class="s">"Nameless"</span><span class="o">)</span>
<span class="o">.</span><span class="na">valuesNonEmpty</span><span class="o">())</span>
<span class="o">.</span><span class="na">clearComment</span><span class="o">()</span>
<span class="o">.</span><span class="na">updateAll</span><span class="o">();</span>
</code></pre></div>
<h2>Usage</h2>
<h3>Dependencies</h3>
<p>In addition to code annotation-processor, it's necessary to add the <code>mongo</code> annotation module and runtime library, including some required transitive dependencies.</p>
<p><a name="dependencies"></a></p>
<ul>
<li><a href="http://search.maven.org/#artifactdetails%7Corg.immutables%7Cmongo%7C2.6.3%7Cjar">org.immutables:mongo:2.6.3</a>
<ul>
<li>Compile and runtime utilities used during marshaling</li>
</ul></li>
</ul>
<p><em>Mongo</em> artifact required to be used for compilation as well be available at runtime.
<em>Mongo</em> module works closely with <a href="/json.html#gson">Gson</a> module, which is also included as transitive dependency.</p>
<p><strong>Note:</strong> Current release works with versions <code>2.12+</code> of the MongoDB Java driver - version <code>3.0</code> and up is not yet supported.</p>
<p>Snippet of Maven dependencies:</p>
<div class="highlight"><pre><code class="language-xml" data-lang="xml"><span></span><span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>value<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.6.3<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>mongo<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.6.3<span class="nt"></version></span>
<span class="nt"></dependency></span>
</code></pre></div>
<h3>Enable repository generation</h3>
<p>In order to enable repository generation, put an <code>org.immutables.mongo.Mongo.Repository</code>
annotation on a abstract value class alongside an <code>org.immutables.value.Value.Immutable</code> annotation.
A repository which accesses a collection of documents will be generated
as a class with a <code>Repository</code> suffix in the same package.</p>
<p>By default, the mapped collection name is derived from abstract value class name: For a
<code>UserDocument</code> class, the collection name will be <code>userDocument</code>. However, the name is customizable
using a <code>value</code> annotation attribute:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="kn">import</span> <span class="nn">org.immutables.mongo.Mongo</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.immutables.value.Value</span><span class="o">;</span>
<span class="nd">@Value.Immutable</span>
<span class="nd">@Gson.TypeAdapters</span> <span class="c1">// you can put TypeAdapters on a package instead</span>
<span class="nd">@Mongo.Repository</span><span class="o">(</span><span class="s">"user"</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">UserDocument</span> <span class="o">{</span>
<span class="o">...</span>
<span class="o">}</span>
</code></pre></div>
<h3>Creating repositories</h3>
<p>Once the repository class is generated, it's possible to instantiate this class using the <code>new</code>
operator. You need to supply a <code>org.immutables.common.repository.RepositorySetup</code> as a
constructor argument. Setup can be shared by all repositories for a single MongoDB
database. <code>RepositorySetup</code> combines the definition of a thread pool, MongoDB database,
and a configured <code>com.google.gson.Gson</code> instance.</p>
<p>Luckily, to get started (and for simpler applications), there's an easy way to create a setup
using <code>RepositorySetup.forUri</code> factory method. Pass a MongoDB connection string and a setup
will be created with default settings.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">RepositorySetup</span> <span class="n">setup</span> <span class="o">=</span> <span class="n">RepositorySetup</span><span class="o">.</span><span class="na">forUri</span><span class="o">(</span><span class="s">"mongodb://localhost/test"</span><span class="o">);</span>
</code></pre></div>
<p>To get a test database running on the default port on a local machine, just launch <code>mongod</code>.</p>
<p>To fully customize setting use <code>RepositorySetup</code> builder:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">MongoClient</span> <span class="n">mongoClient</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">ListeningExecutorService</span> <span class="n">executor</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">GsonBuilder</span> <span class="n">gsonBuilder</span> <span class="o">=</span> <span class="k">new</span> <span class="n">GsonBuilder</span><span class="o">();</span>
<span class="o">...</span>
<span class="n">RepositorySetup</span> <span class="n">setup</span> <span class="o">=</span> <span class="n">RepositorySetup</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">database</span><span class="o">(</span><span class="n">mongoClient</span><span class="o">.</span><span class="na">getDB</span><span class="o">(</span><span class="s">"test"</span><span class="o">))</span>
<span class="o">.</span><span class="na">executor</span><span class="o">(</span><span class="n">executor</span><span class="o">)</span>
<span class="o">.</span><span class="na">gson</span><span class="o">(</span><span class="n">gsonBuilder</span><span class="o">.</span><span class="na">create</span><span class="o">())</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span>
</code></pre></div>
<p>See <a href="http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/">getting started with java driver</a> for an explanation how to create a <code>MongoClient</code>.</p>
<h3>Id attribute</h3>
<p>It is highly recommended to have explicit <code>_id</code> field for MongoDB documents. Use the <code>@Mongo.Id</code>
annotation to declare an <code>id</code> attribute. The <code>@Mongo.Id</code> annotation acts as an alias to
<code>@Gson.Named("_id")</code>, which can also be used.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="nd">@Value.Immutable</span>
<span class="nd">@Gson.TypeAdapters</span>
<span class="nd">@Mongo.Repository</span><span class="o">(</span><span class="s">"user"</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">UserDocument</span> <span class="o">{</span>
<span class="nd">@Mongo.Id</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">int</span> <span class="nf">id</span><span class="o">();</span>
<span class="o">...</span>
<span class="o">}</span>
</code></pre></div>
<p>An identifier attribute can be of any type that is marshaled to a valid BSON type that can
be used as <code>_id</code> field in MongoDB. The Java attribute name is irrelevant as long as it will be
generated marshaled as <code>_id</code> (annotated with <code>@Gson.Named("_id")</code> or <code>@Mongo.Id</code>).</p>
<p>In some cases you may need to use special type <code>ObjectID</code> for <code>_id</code> or other fields. In order to
do this, <em>Immutables</em> provides the wrapper type <code>org.immutables.mongo.types.Id</code>. Use the static
factory methods of <code>org.immutables.mongo.types.Id</code> class to construct instances that correspond
to MongoDB' <code>ObjectID</code>. Here's example of an auto-generated identifier:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="kn">import</span> <span class="nn">org.immutables.value.Value</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.immutables.gson.Gson</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.immutables.mongo.Mongo</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.immutables.mongo.types.Id</span><span class="o">;</span>
<span class="nd">@Value.Immutable</span>
<span class="nd">@Gson.TypeAdapters</span>
<span class="nd">@Mongo.Repository</span><span class="o">(</span><span class="s">"events"</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">EventRecord</span> <span class="o">{</span>
<span class="nd">@Mongo.Id</span>
<span class="nd">@Value.Default</span>
<span class="kd">public</span> <span class="n">Id</span> <span class="nf">id</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">Id</span><span class="o">.</span><span class="na">generate</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="o">}</span>
</code></pre></div>
<hr>
<h2>BSON/JSON documents</h2>
<p>All values used to model documents should have GSON type adapters registered. Use
<code>@Gson.TypeAdapters</code> on types or packages to generate type adapters for enclosed value types. When
using <code>RepositorySetup.forUri</code>, all type adapters will be auto-registered from the classpath. When
using custom <code>RepositorySetup</code>, register type adapters on a <code>Gson</code> instance using <code>GsonBuilder</code>
as shown in <a href="json.html#adapter-registration">GSON guide</a>.</p>
<p>A large portion of the things you need to know to create MongoDB mapped documents is described in
<a href="json.html#gson">GSON guide</a></p>
<hr>
<h2>Operations</h2>
<h4>Sample document repository</h4>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="nd">@Value.Immutable</span>
<span class="nd">@Gson.TypeAdapters</span>
<span class="nd">@Mongo.Repository</span><span class="o">(</span><span class="s">"posts"</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">PostDocument</span> <span class="o">{</span>
<span class="nd">@Mongo.Id</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">long</span> <span class="nf">id</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">String</span> <span class="nf">content</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">List</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="nf">ratings</span><span class="o">();</span>
<span class="nd">@Value.Default</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">version</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="mi">0</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Instantiate generated repository</span>
<span class="n">PostDocumentRepository</span> <span class="n">posts</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PostDocumentRepository</span><span class="o">(</span>
<span class="n">RepositorySetup</span><span class="o">.</span><span class="na">forUri</span><span class="o">(</span><span class="s">"mongodb://localhost/test"</span><span class="o">));</span>
</code></pre></div>
<h3>Insert documents</h3>
<p>Insert single or iterable of documents using <code>insert</code> methods.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">posts</span><span class="o">.</span><span class="na">insert</span><span class="o">(</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"a"</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">());</span>
<span class="n">posts</span><span class="o">.</span><span class="na">insert</span><span class="o">(</span>
<span class="n">ImmutableList</span><span class="o">.</span><span class="na">of</span><span class="o">(</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"b"</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">(),</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">3</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"c"</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">(),</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">4</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"d"</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">()));</span>
</code></pre></div>
<h3>Upsert document</h3>
<p>Update or insert full document content by <code>_id</code> using the <code>upsert</code> method:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">posts</span><span class="o">.</span><span class="na">upsert</span><span class="o">(</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"a1"</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">());</span>
<span class="n">posts</span><span class="o">.</span><span class="na">upsert</span><span class="o">(</span>
<span class="n">ImmutablePostDocument</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="mi">10</span><span class="o">)</span>
<span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"!!!"</span><span class="o">)</span>
<span class="o">.</span><span class="na">addRatings</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">());</span>
</code></pre></div>
<p>If document with <code>_id</code> 10 is not found, then it will be created, otherwise updated.</p>
<h3>Find documents</h3>
<p>To find a document, you need to provide criteria object. Search criteria objects are generated to reflect fields of
the document. Empty criteria objects are obtained by using the <code>criteria()</code> static factory method on a generated repository.
Criteria objects are immutable and can be stored as constants or otherwise safely passed around.
Criteria objects have methods corresponding to document attributes and relevant constraints.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">Criteria</span> <span class="n">where</span> <span class="o">=</span> <span class="n">posts</span><span class="o">.</span><span class="na">criteria</span><span class="o">();</span>
<span class="n">List</span><span class="o"><</span><span class="n">PostDocument</span><span class="o">></span> <span class="n">documents</span> <span class="o">=</span>
<span class="n">posts</span><span class="o">.</span><span class="na">find</span><span class="o">(</span><span class="n">where</span><span class="o">.</span><span class="na">contentStartsWith</span><span class="o">(</span><span class="s">"a"</span><span class="o">))</span>
<span class="o">.</span><span class="na">fetchAll</span><span class="o">()</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
<span class="n">Optional</span><span class="o"><</span><span class="n">PostDocument</span><span class="o">></span> <span class="n">document</span> <span class="o">=</span>
<span class="n">posts</span><span class="o">.</span><span class="na">find</span><span class="o">(</span>
<span class="n">where</span><span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">"!!!"</span><span class="o">)</span>
<span class="o">.</span><span class="na">ratingsNonEmpty</span><span class="o">())</span>
<span class="o">.</span><span class="na">fetchFirst</span><span class="o">()</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
<span class="n">List</span><span class="o"><</span><span class="n">PostDocument</span><span class="o">></span> <span class="n">limited</span> <span class="o">=</span>
<span class="n">posts</span><span class="o">.</span><span class="na">find</span><span class="o">(</span>
<span class="n">where</span><span class="o">.</span><span class="na">contentStartsWith</span><span class="o">(</span><span class="s">"a"</span><span class="o">)</span>
<span class="o">.</span><span class="na">or</span><span class="o">()</span>
<span class="o">.</span><span class="na">contentStartsWith</span><span class="o">(</span><span class="s">"b"</span><span class="o">))</span>
<span class="o">.</span><span class="na">orderById</span><span class="o">()</span>
<span class="o">.</span><span class="na">skip</span><span class="o">(</span><span class="mi">5</span><span class="o">)</span>
<span class="o">.</span><span class="na">fetchWithLimit</span><span class="o">(</span><span class="mi">10</span><span class="o">)</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
</code></pre></div>
<p>With each constraint, a new immutable criteria is returned which composes constraints with the <em>and</em> logic. Constraints
can be composed with <em>or</em> logic by explicitly delimiting with <code>.or()</code> method,
effectively forming <a href="http://en.wikipedia.org/wiki/Disjunctive_normal_form">DNF</a> consisting of constraints.</p>
<p>The <code>find</code> method returns an uncompleted operation, which is subject to configuration via <code>Finder</code> object methods,
discover these configuration methods, use them as needed, then invoke finalizing operation which returns <em>future</em>
of result.</p>
<h4>Simple find methods</h4>
<p>For convenience, there are methods to lookup by <code>_id</code> and to find all documents. These methods do not need criteria objects.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">posts</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">10</span><span class="o">).</span><span class="na">fetchFirst</span><span class="o">();</span>
<span class="c1">// Fetch all? Ok</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findAll</span><span class="o">().</span><span class="na">fetchAll</span><span class="o">();</span>
</code></pre></div>
<p>Note that <code>findById</code> method might be named differently if your document has its attribute with a name other than <code>id</code>
in Java.</p>
<h3>Excluding output fields</h3>
<p>MongoDB has a feature to return a subset of fields in results. In order to preserve the consistency of immutable
document objects created during unmarshaling, a repository only allows the exclusion of optional fields such
as <a href="immutable.html#collection">collection attributes</a> and <a href="immutable.html#optional">optional attributes</a>.
Use <code>exclude*</code> methods on <code>Finder</code> objects to configure attribute exclusion.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="kt">boolean</span> <span class="n">isTrue</span> <span class="o">=</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">10</span><span class="o">)</span>
<span class="o">.</span><span class="na">excludeRatings</span><span class="o">()</span>
<span class="o">.</span><span class="na">fetchFirst</span><span class="o">()</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">()</span>
<span class="o">.</span><span class="na">ratings</span><span class="o">()</span>
<span class="o">.</span><span class="na">isEmpty</span><span class="o">();</span>
</code></pre></div>
<h3>Sorting result</h3>
<p>Use <code>Finder</code> to specify ordering by attributes and direction. Ordering is used for fetching results as well
as finding the first matching object to modify.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">posts</span><span class="o">.</span><span class="na">find</span><span class="o">(</span><span class="n">where</span><span class="o">.</span><span class="na">contentNot</span><span class="o">(</span><span class="s">"b"</span><span class="o">))</span>
<span class="o">.</span><span class="na">orderByContent</span><span class="o">()</span>
<span class="o">.</span><span class="na">orderByIdDesceding</span><span class="o">()</span>
<span class="o">.</span><span class="na">deleteFirst</span><span class="o">();</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findAll</span><span class="o">()</span>
<span class="o">.</span><span class="na">orderByContent</span><span class="o">()</span>
<span class="o">.</span><span class="na">fetchWithLimit</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span>
</code></pre></div>
<h3>Delete documents</h3>
<p>Looking for delete operations? Well, we found good place for them, but probably not a very obvious one!</p>
<p>Delete operations are defined on the same <code>Finder</code> object:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">posts</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">1</span><span class="o">).</span><span class="na">deleteFirst</span><span class="o">();</span>
<span class="kt">int</span> <span class="n">deletedDocumentsCount</span> <span class="o">=</span> <span class="n">posts</span><span class="o">.</span><span class="na">find</span><span class="o">(</span><span class="n">where</span><span class="o">.</span><span class="na">content</span><span class="o">(</span><span class="s">""</span><span class="o">))</span>
<span class="o">.</span><span class="na">deleteAll</span><span class="o">()</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
<span class="c1">// Delete all? Ok</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findAll</span><span class="o">().</span><span class="na">deleteAll</span><span class="o">();</span>
</code></pre></div>
<h3>Update and FindAndModify</h3>
<p>Update, find, and modify operations support incremental updates of the documents matching a criteria.
Incremental update operations are used to update particular fields. Some fields may need to be initialized
if a document is to be created via upsert operation.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="n">Optional</span><span class="o"><</span><span class="n">PostDocument</span><span class="o">></span> <span class="n">updatedDocument</span> <span class="o">=</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">andModifyFirst</span><span class="o">()</span>
<span class="o">.</span><span class="na">addRatings</span><span class="o">(</span><span class="mi">5</span><span class="o">)</span>
<span class="o">.</span><span class="na">setContent</span><span class="o">(</span><span class="s">"bbb"</span><span class="o">)</span>
<span class="o">.</span><span class="na">returningNew</span><span class="o">()</span>
<span class="o">.</span><span class="na">update</span><span class="o">()</span>
<span class="o">.</span><span class="na">getUnchecked</span><span class="o">();</span>
<span class="n">posts</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">where</span><span class="o">.</span><span class="na">ratingsEmpty</span><span class="o">())</span>
<span class="o">.</span><span class="na">addRatings</span><span class="o">(</span><span class="mi">3</span><span class="o">)</span>
<span class="o">.</span><span class="na">updateAll</span><span class="o">();</span>
<span class="n">posts</span><span class="o">.</span><span class="na">findById</span><span class="o">(</span><span class="mi">111</span><span class="o">)</span>
<span class="o">.</span><span class="na">andModifyFirst</span><span class="o">()</span>
<span class="o">.</span><span class="na">incrementVersion</span><span class="o">(</span><span class="mi">1</span><span class="o">)</span>
<span class="o">.</span><span class="na">initContent</span><span class="o">(</span><span class="s">"2"</span><span class="o">)</span>
<span class="o">.</span><span class="na">addRatings</span><span class="o">(</span><span class="mi">5</span><span class="o">)</span>
<span class="o">.</span><span class="na">upsert</span><span class="o">();</span>
</code></pre></div>
<h3>Ensure Index</h3>
<p>If you want to ensure indices using code rather than the administrative tools,
you can use an <code>Indexer</code> object, which ensures indexing with particular fields.
See the methods of <code>Indexer</code> object.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><span class="c1">// Compound index on content and ratings</span>
<span class="n">posts</span><span class="o">.</span><span class="na">index</span><span class="o">()</span>
<span class="o">.</span><span class="na">withContent</span><span class="o">()</span>
<span class="o">.</span><span class="na">withRatings</span><span class="o">()</span>
<span class="o">.</span><span class="na">ensure</span><span class="o">();</span>
<span class="c1">// Reversed index on content</span>
<span class="n">posts</span><span class="o">.</span><span class="na">index</span><span class="o">()</span>
<span class="o">.</span><span class="na">withContentDesceding</span><span class="o">()</span>
<span class="o">.</span><span class="na">ensure</span><span class="o">();</span>
</code></pre></div>
</section>
<footer class="jumbotron">
<div class="container">
<h2>Guides</h2>
<ul>
<li><a href="/getstarted.html">Get started!</a></li>
<li><a href="/intro.html">Inception</a></li>
<li><a href="/immutable.html">Immutable objects</a></li>
<li><a href="/factory.html">Factory builders</a></li>
<li><a href="/functional.html">Functions and Predicates (for Java 7)</a></li>
<li><a href="/style.html">Style customization</a></li>
<li><a href="/json.html">JSON serialization</a></li>
<li><a href="/mongo.html">MongoDB repositories</a></li>
<li><a href="/encoding.html">Encoding: Customizing attributes and builders (experimental)</a></li>
<li><a href="/apt.html">Using annotation processor in IDE</a></li>
</ul>
<h2>Get involved</h2>
<ul>
<li>Clone source repository, contribute bug reports and fixes on <a href="https://github.com/immutables/immutables">GitHub immutables/immutables</a></li>
<li>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li>
<li>Ask questions or give feedback and ideas using mailing group <a href="https://groups.google.com/forum/#!forum/immutables">immutables at googlegroups.com</a></li>
</ul>
<p><a href="/license.html">Apache License 2.0</a></p>
<!--<div><h2>Posts</h2>
<ul>
</ul>
</div>-->
</div>
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script defer src="/gfx/jquery.toc.min.js"></script>
<script>
$(function() {
$('#toc').toc({
container: '.documentation',
selectors: 'h1,h2,h3,h4',
anchorName: function(i, heading, prefix) {
heading = $(heading).text();
if (heading.trim) heading = heading.trim();
return heading.toLowerCase().replace(/ /g, '-').replace(/[^a-z^\-]+/g, '');
},
})
})
</script>
<script async defer id="github-bjs" src="https://buttons.github.io/buttons.js"></script>
</body>
</html>