@@ -19,35 +19,66 @@ Builders
19
19
:depth: 2
20
20
:class: singlecol
21
21
22
- This section includes guides on how to use each of the available builders, and demonstrates the utility the Java driver
23
- builder classes provide.
22
+ This section includes guides on how to use each of the available
23
+ builders, and demonstrates the utility the Java driver builder classes
24
+ provide.
24
25
25
26
Overview
26
27
--------
27
28
28
- The Java driver provides classes to make it easier to use CRUD operations and the Aggregation API. The static utility methods
29
- allow you to build a query more succinctly.
29
+ The Java driver provides classes to simplify the process for developers
30
+ to use CRUD operations and the Aggregation API. The static utility methods allow you
31
+ to build a query more efficiently.
30
32
33
+ Why Use Builders?
34
+ -----------------
31
35
32
- Imagine we want to send a marketing email to all users in our users collection that identify as "female" gender and are older than "29".
33
- We only need their email address, so we'll ensure our query doesn't return data we pay bandwidth
34
- costs for but don't need.
36
+ Using the builders class, you leverage the power of:
35
37
36
- Using JSON and the MongoDB shell, the query would look something like:
38
+ - The Java compiler and the IDE to find errors during development
39
+ - The IDE for discovery and code completion
40
+
41
+ The Java compiler and the IDE catch errors such as misspelled
42
+ operators early on. When using the MongoDB shell or plain Java, you
43
+ write operators as strings and get no visual indication of a problem,
44
+ pushing these errors to runtime instead of compile time
45
+
46
+ With the builder classes, you write operators as methods. The IDE
47
+ instantly underlines and gives you a red bar on the right indicating
48
+ something is wrong. While developing, the IDE also shows you with the
49
+ methods you can use. It automatically completes your code with
50
+ placeholder parameters once you select which method you want to use.
51
+
52
+ Scenario
53
+ --------
54
+
55
+ Imagine we want to send a marketing email to all users in our ``users``
56
+ collection with the following criteria:
57
+
58
+ - Users that identify as "female" gender
59
+ - Users that are older than "29"
60
+
61
+ We only want their email address, so we'll ensure our query doesn't
62
+ return data we pay bandwidth costs for but don't need.
63
+
64
+ Using the MongoDB Shell
65
+ ~~~~~~~~~~~~~~~~~~~~~~~
37
66
38
67
.. code-block:: js
39
68
40
69
collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 })
41
70
42
- Without builders, we construct the query in Java as:
71
+ Without Using Builders
72
+ ~~~~~~~~~~~~~~~~~~~~~~
43
73
44
74
.. code-block:: java
45
75
46
- Bson filter = new Document().append("gender", "female").append("age", new Document().append("$gt", 28 ));
76
+ Bson filter = new Document().append("gender", "female").append("age", new Document().append("$gt", 29 ));
47
77
Bson projection = new Document().append("_id", 0).append("email", 1);
48
78
collection.find(filter).projection(projection);
49
79
50
- With builders, the query becomes:
80
+ Using Builders
81
+ ~~~~~~~~~~~~~~
51
82
52
83
.. code-block:: java
53
84
0 commit comments