@@ -19,35 +19,66 @@ Builders
1919 :depth: 2
2020 :class: singlecol
2121
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.
2425
2526Overview
2627--------
2728
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.
3032
33+ Why Use Builders?
34+ -----------------
3135
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:
3537
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+ ~~~~~~~~~~~~~~~~~~~~~~~
3766
3867.. code-block:: js
3968
4069 collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 })
4170
42- Without builders, we construct the query in Java as:
71+ Without Using Builders
72+ ~~~~~~~~~~~~~~~~~~~~~~
4373
4474.. code-block:: java
4575
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 ));
4777 Bson projection = new Document().append("_id", 0).append("email", 1);
4878 collection.find(filter).projection(projection);
4979
50- With builders, the query becomes:
80+ Using Builders
81+ ~~~~~~~~~~~~~~
5182
5283.. code-block:: java
5384
0 commit comments