1
1
.. _kotlin-sync-builders:
2
2
3
- ================
4
- Use Builders API
5
- ================
3
+ ===============================
4
+ Implement Builders Code Pattern
5
+ ===============================
6
6
7
7
.. contents:: On this page
8
8
:local:
@@ -22,29 +22,31 @@ Use Builders API
22
22
Overview
23
23
--------
24
24
25
- This page describes how to use the builders API and demonstrates the utility provided by
26
- builder classes.
25
+ This page describes how to implement the builders pattern in your code and describes
26
+ benefits of using builder classes.
27
27
28
- The {+driver-short+} provides type-safe classes and methods that enable developers to
29
- efficiently build queries and aggregations.
28
+ The {+driver-short+} provides type-safe builders classes and methods that enable developers to
29
+ efficiently build queries and aggregations. To learn more about the builders pattern,
30
+ see `Type-safe builders <https://kotlinlang.org/docs/type-safe-builders.html>`__ in the
31
+ {+language+} documentation.
30
32
31
- Why Use Builders?
32
- -----------------
33
+ Why Use the Builders Code Pattern ?
34
+ ----------------------------------
33
35
34
- When using the MongoDB shell or plain Kotlin to construct BSON query documents, you are not
36
+ If you use only plain {+language+} to construct BSON query documents, you are not
35
37
able to identify errors in your code until runtime.
36
38
37
- With builder classes, you can write operators as methods and any syntax errors will be
38
- caught at compile time.
39
+ When you use builder classes, you can write operators as methods and any syntax errors
40
+ will be caught at compile time.
39
41
40
42
Example
41
43
-------
42
44
43
- This section describes three methods of fetching the ``email`` field values of documents
45
+ This section provides three equivalent ways to fetch the ``email`` field values of documents
44
46
in the ``users`` collection that meet the following criteria:
45
47
46
- - Users that identify as `` female``
47
- - Users that are older than ``29``
48
+ - ``gender`` value is ``" female" ``
49
+ - ``age`` value is greateer than ``29``
48
50
49
51
The following data class models the documents in the ``users`` collection:
50
52
@@ -64,19 +66,20 @@ The following data class models the results returned by our query:
64
66
:copyable:
65
67
:dedent:
66
68
67
- Using the MongoDB Shell
68
- ~~~~~~~~~~~~~~~~~~~~~~~
69
+ MongoDB Query API
70
+ ~~~~~~~~~~~~~~~~~
69
71
70
- The following sample executes the previously defined query using the MongoDB shell :
72
+ The following sample performs the query by using the MongoDB Query API :
71
73
72
74
.. code-block:: js
73
75
74
76
collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 })
75
77
76
- Without Using Builders
77
- ~~~~~~~~~~~~~~~~~~~~~~
78
+ Document Class Filter
79
+ ~~~~~~~~~~~~~~~~~~~~~
78
80
79
- The following example executes the previously defined query using plain {+language+}:
81
+ The following example performs the query by using the ``Document`` class to construct the
82
+ query filter:
80
83
81
84
.. literalinclude:: /includes/builders/builders.kt
82
85
:start-after: start-find
@@ -85,10 +88,10 @@ The following example executes the previously defined query using plain {+langua
85
88
:copyable:
86
89
:dedent:
87
90
88
- Using Builders
89
- ~~~~~~~~~~~~~~
91
+ Builders Pattern
92
+ ~~~~~~~~~~~~~~~~
90
93
91
- The following example executes the previously defined query using the builders API :
94
+ The following example performs the query by using the builders pattern :
92
95
93
96
.. literalinclude:: /includes/builders/builders.kt
94
97
:start-after: start-find-builders
@@ -102,6 +105,9 @@ The following example executes the previously defined query using the builders A
102
105
.. Available Builders
103
106
.. ------------------
104
107
108
+ .. The following pages describe how to implement the different classes of builders
109
+ .. available in the {+driver-short+}.
110
+
105
111
.. - :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines.
106
112
.. - :ref:`Filters <filters-builders>` for building query filters.
107
113
.. - :ref:`Indexes <indexes-builders>` for creating index keys.
0 commit comments