|
| 1 | +.. _kotlin-sync-builders: |
| 2 | + |
| 3 | +========================= |
| 4 | +Use Builders Code Pattern |
| 5 | +========================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. .. toctree:: |
| 14 | + |
| 15 | +.. /fundamentals/builders/aggregates |
| 16 | +.. /fundamentals/builders/filters |
| 17 | +.. /fundamentals/builders/indexes |
| 18 | +.. /fundamentals/builders/projections |
| 19 | +.. /fundamentals/builders/sort |
| 20 | +.. /fundamentals/builders/updates |
| 21 | + |
| 22 | +Overview |
| 23 | +-------- |
| 24 | + |
| 25 | +This page describes how to use the various available builders in your code and describes |
| 26 | +benefits of using the provided builders. |
| 27 | + |
| 28 | +The {+driver-short+} provides type-safe builder classes and methods that enable developers to |
| 29 | +efficiently build queries and aggregations. |
| 30 | + |
| 31 | +Why Use Builders? |
| 32 | +----------------- |
| 33 | + |
| 34 | +If you use only plain {+language+} to construct BSON query documents, you are not |
| 35 | +able to identify syntax errors until runtime. The builders help ensure the corretness of |
| 36 | +syntax and can be less verbose than constructing BSON documents. |
| 37 | + |
| 38 | +Example |
| 39 | +------- |
| 40 | + |
| 41 | +This section provides three equivalent ways to fetch the ``email`` field values of documents |
| 42 | +in the ``users`` collection that meet the following criteria: |
| 43 | + |
| 44 | +- ``gender`` value is ``"female"`` |
| 45 | +- ``age`` value is greater than ``29`` |
| 46 | + |
| 47 | +The following data class models the documents in the ``users`` collection: |
| 48 | + |
| 49 | +.. literalinclude:: /includes/builders/builders.kt |
| 50 | + :start-after: start-user-class |
| 51 | + :end-before: end-user-class |
| 52 | + :language: kotlin |
| 53 | + :copyable: |
| 54 | + :dedent: |
| 55 | + |
| 56 | +The following data class models the results returned by our query: |
| 57 | + |
| 58 | +.. literalinclude:: /includes/builders/builders.kt |
| 59 | + :start-after: start-result-class |
| 60 | + :end-before: end-result-class |
| 61 | + :language: kotlin |
| 62 | + :copyable: |
| 63 | + :dedent: |
| 64 | + |
| 65 | +MongoDB Query API |
| 66 | +~~~~~~~~~~~~~~~~~ |
| 67 | + |
| 68 | +The following sample performs the query by using the MongoDB Query API: |
| 69 | + |
| 70 | +.. code-block:: js |
| 71 | + |
| 72 | + collection.find( |
| 73 | + { "gender": "female", "age" : { "$gt": 29 }}, |
| 74 | + { "_id": 0, "email": 1 } |
| 75 | + ) |
| 76 | + |
| 77 | +Document Class Filter |
| 78 | +~~~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +The following example performs the query by using the ``Document`` class to construct the |
| 81 | +query filter: |
| 82 | + |
| 83 | +.. literalinclude:: /includes/builders/builders.kt |
| 84 | + :start-after: start-find |
| 85 | + :end-before: end-find |
| 86 | + :language: kotlin |
| 87 | + :copyable: |
| 88 | + :dedent: |
| 89 | + |
| 90 | +Builders |
| 91 | +~~~~~~~~ |
| 92 | + |
| 93 | +The following example performs the query by using the builder helpers: |
| 94 | + |
| 95 | +.. literalinclude:: /includes/builders/builders.kt |
| 96 | + :start-after: start-find-builders |
| 97 | + :end-before: end-find-builders |
| 98 | + :language: kotlin |
| 99 | + :copyable: |
| 100 | + :dedent: |
| 101 | + |
| 102 | +.. TODO: Uncomment as pages get built |
| 103 | + |
| 104 | +.. Available Builders |
| 105 | +.. ------------------ |
| 106 | + |
| 107 | +.. The following pages describe how to implement the different classes of builders |
| 108 | +.. available in the {+driver-short+}. |
| 109 | + |
| 110 | +.. - :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines. |
| 111 | +.. - :ref:`Filters <filters-builders>` for building query filters. |
| 112 | +.. - :ref:`Indexes <indexes-builders>` for creating index keys. |
| 113 | +.. - :ref:`Projections <projections-builders>` for building projections. |
| 114 | +.. - :ref:`Sorts <sorts-builders>` for building sort criteria. |
| 115 | +.. - :ref:`Updates <updates-builders>` for building updates. |
0 commit comments