|
| 1 | +.. _kotlin-sync-builders-data-classes: |
| 2 | + |
| 3 | +============================== |
| 4 | +Use Builders with Data Classes |
| 5 | +============================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, data format, modeling, interoperability |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use your :ref:`Data Class |
| 24 | +<kotlin-sync-data-format-data-classes>` properties directly with the |
| 25 | +builder classes available in the {+driver-short+}. |
| 26 | + |
| 27 | +The {+driver-short+} implements extensions that allow you to reference |
| 28 | +your data class properties when using builder methods instead of using |
| 29 | +string field names. You can structure your code in this way to make your |
| 30 | +code more type-safe and improve your applications {+language+} |
| 31 | +interoperability. |
| 32 | + |
| 33 | +The extensions library also allows you to construct |
| 34 | +queries, update documents, and write other statements by using infix |
| 35 | +notation. To learn more about this notation, see `Infix notation |
| 36 | +<{+kotlin-docs+}/docs/functions.html#infix-notation>`__ in the |
| 37 | +{+language+} reference documentation. |
| 38 | + |
| 39 | +.. note:: |
| 40 | + |
| 41 | + This page provides a limited number of code |
| 42 | + examples to demonstrate this functionality. To learn more about using |
| 43 | + builder classes, see the :ref:`kotlin-sync-builders` guide. |
| 44 | + |
| 45 | +Add {+language+} Extensions to Your Project |
| 46 | +------------------------------------- |
| 47 | + |
| 48 | +To implement this functionality, you must add the |
| 49 | +``mongodb-driver-kotlin-extensions`` dependency to your dependencies |
| 50 | +list. |
| 51 | + |
| 52 | +Select from the following tabs to see how to add the extension |
| 53 | +dependency to your project by using the :guilabel:`Gradle` and |
| 54 | +:guilabel:`Maven` package managers: |
| 55 | + |
| 56 | +.. tabs:: |
| 57 | + |
| 58 | + .. tab:: |
| 59 | + :tabid: Gradle |
| 60 | + |
| 61 | + If you are using `Gradle <https://gradle.org/>`__ to manage your |
| 62 | + dependencies, add the following to your ``build.gradle.kts`` dependencies list: |
| 63 | + |
| 64 | + .. code-block:: kotlin |
| 65 | + :caption: build.gradle.kts |
| 66 | + |
| 67 | + implementation("org.mongodb:mongodb-driver-kotlin-extensions:{+full-version+}") |
| 68 | + |
| 69 | + .. tab:: |
| 70 | + :tabid: Maven |
| 71 | + |
| 72 | + If you are using `Maven <https://maven.apache.org/>`__ to manage your |
| 73 | + dependencies, add the following to your ``pom.xml`` dependencies list: |
| 74 | + |
| 75 | + .. code-block:: xml |
| 76 | + :caption: pom.xml |
| 77 | + |
| 78 | + <dependency> |
| 79 | + <groupId>org.mongodb</groupId> |
| 80 | + <artifactId>mongodb-driver-kotlin-extensions</artifactId> |
| 81 | + <version>{+full-version+}</version> |
| 82 | + </dependency> |
| 83 | + |
| 84 | +After you install the extensions dependency, you can use the extension |
| 85 | +methods by importing classes and methods from the |
| 86 | +``com.mongodb.kotlin.client.model`` path. You can mix usage of these methods and |
| 87 | +the standard builder methods in the same application, as shown in the |
| 88 | +:ref:`kotlin-sync-data-class-aggregates` example in this guide. |
| 89 | + |
| 90 | +Builders Examples |
| 91 | +----------------- |
| 92 | + |
| 93 | +This section contains examples that demonstrate how to use data class |
| 94 | +properties directly with builder class methods from the extensions |
| 95 | +package. |
| 96 | + |
| 97 | +.. tip:: Data Class Annotations |
| 98 | + |
| 99 | + When you the extension builder class methods data |
| 100 | + classes, the methods respect your data class annotations from the |
| 101 | + ``bson-kotlin`` and ``bson-kotlinx`` packages. To learn more about |
| 102 | + annotations, see the :ref:`kotlin-sync-data-class-annotations` |
| 103 | + section of the Document Data Format: Data Classes guide. |
| 104 | + |
| 105 | +Sample Data |
| 106 | +~~~~~~~~~~~ |
| 107 | + |
| 108 | +The examples in this section use documents in the ``students`` |
| 109 | +collection that describe students at a school. Documents in the |
| 110 | +``students`` collection are modeled by the following {+language+} data |
| 111 | +class: |
| 112 | + |
| 113 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 114 | + :language: kotlin |
| 115 | + :start-after: start-data-class |
| 116 | + :end-before: end-data-class |
| 117 | + :dedent: |
| 118 | + |
| 119 | +.. _kotlin-sync-data-class-filters: |
| 120 | + |
| 121 | +Filters |
| 122 | +~~~~~~~ |
| 123 | + |
| 124 | +You can use helpers from the ``Filters`` builders class to query on data |
| 125 | +class properties. |
| 126 | + |
| 127 | +The following code shows different ways to use ``Filters`` extension |
| 128 | +methods to perform queries on the ``Student`` data class: |
| 129 | + |
| 130 | +.. code-block:: kotlin |
| 131 | + |
| 132 | + import com.mongodb.kotlin.client.model.Filters.eq |
| 133 | + import com.mongodb.kotlin.client.model.Filters.all |
| 134 | + |
| 135 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 136 | + :language: kotlin |
| 137 | + :start-after: start-filters-data-class |
| 138 | + :end-before: end-filters-data-class |
| 139 | + :dedent: |
| 140 | + |
| 141 | +.. _kotlin-sync-data-class-indexes: |
| 142 | + |
| 143 | +Indexes |
| 144 | +~~~~~~~ |
| 145 | + |
| 146 | +You can use helpers from the ``Indexes`` builders class to create |
| 147 | +indexes on data class properties. |
| 148 | + |
| 149 | +The following code shows different ways to use ``Indexes`` extension |
| 150 | +methods to create indexes on the ``Student`` data class: |
| 151 | + |
| 152 | +.. code-block:: kotlin |
| 153 | + |
| 154 | + import com.mongodb.kotlin.client.model.Indexes.ascending |
| 155 | + import com.mongodb.kotlin.client.model.Indexes.descending |
| 156 | + |
| 157 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 158 | + :language: kotlin |
| 159 | + :start-after: start-indexes-data-class |
| 160 | + :end-before: end-indexes-data-class |
| 161 | + :dedent: |
| 162 | + |
| 163 | +.. _kotlin-sync-data-class-projections: |
| 164 | + |
| 165 | +Projections |
| 166 | +~~~~~~~~~~~ |
| 167 | + |
| 168 | +You can use helpers from the ``Projections`` builders class to create |
| 169 | +projections for data class properties. |
| 170 | + |
| 171 | +The following code shows how to use ``Projections`` extension |
| 172 | +methods to create a projection on the ``Student`` data class: |
| 173 | + |
| 174 | +.. code-block:: kotlin |
| 175 | + |
| 176 | + import com.mongodb.kotlin.client.model.Projections.excludeId |
| 177 | + import com.mongodb.kotlin.client.model.Projections.fields |
| 178 | + import com.mongodb.kotlin.client.model.Projections.include |
| 179 | + |
| 180 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 181 | + :language: kotlin |
| 182 | + :start-after: start-proj-data-class |
| 183 | + :end-before: end-proj-data-class |
| 184 | + :dedent: |
| 185 | + |
| 186 | +.. _kotlin-sync-data-class-sorts: |
| 187 | + |
| 188 | +Sorts |
| 189 | +~~~~~ |
| 190 | + |
| 191 | +You can use helpers from the ``Sorts`` builders class to sort |
| 192 | +on your data class properties. |
| 193 | + |
| 194 | +The following code shows how to use ``Sorts`` extension |
| 195 | +methods to create different sorts on the ``Student`` data class: |
| 196 | + |
| 197 | +.. code-block:: kotlin |
| 198 | + |
| 199 | + import com.mongodb.client.model.Sorts.orderBy |
| 200 | + import com.mongodb.kotlin.client.model.Sorts |
| 201 | + |
| 202 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 203 | + :language: kotlin |
| 204 | + :start-after: start-sorts-data-class |
| 205 | + :end-before: end-sorts-data-class |
| 206 | + :dedent: |
| 207 | + |
| 208 | +.. _kotlin-sync-data-class-updates: |
| 209 | + |
| 210 | +Updates |
| 211 | +~~~~~~~ |
| 212 | + |
| 213 | +You can use helpers from the ``Updates`` builders class to perform |
| 214 | +updates by using your data class properties. |
| 215 | + |
| 216 | +The following code shows how to use ``Sorts`` extension |
| 217 | +methods to create different sorts on the ``Student`` data class: |
| 218 | + |
| 219 | +.. code-block:: kotlin |
| 220 | + |
| 221 | + import com.mongodb.kotlin.client.model.Filters.gte |
| 222 | + import com.mongodb.kotlin.client.model.Updates.addToSet |
| 223 | + import com.mongodb.kotlin.client.model.Updates.combine |
| 224 | + import com.mongodb.kotlin.client.model.Updates.max |
| 225 | + |
| 226 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 227 | + :language: kotlin |
| 228 | + :start-after: start-updates-data-class |
| 229 | + :end-before: end-updates-data-class |
| 230 | + :dedent: |
| 231 | + |
| 232 | +.. _kotlin-sync-data-class-aggregates: |
| 233 | + |
| 234 | +Aggregates |
| 235 | +~~~~~~~~~~ |
| 236 | + |
| 237 | +You can use helpers from the ``Aggregates`` and ``Accumulators`` |
| 238 | +builders classes to perform aggregations by using you data class |
| 239 | +properties. |
| 240 | + |
| 241 | +The following code shows how to use ``Accumulators`` extension |
| 242 | +methods and ``Aggregates`` helper methods to perform an aggregation on |
| 243 | +the ``Student`` data class: |
| 244 | + |
| 245 | +.. code-block:: kotlin |
| 246 | + |
| 247 | + import com.mongodb.client.model.Aggregates.group |
| 248 | + import com.mongodb.client.model.Aggregates.limit |
| 249 | + import com.mongodb.client.model.Aggregates.sort |
| 250 | + import com.mongodb.kotlin.client.model.Accumulators.avg |
| 251 | + |
| 252 | +.. literalinclude:: /includes/builders/builders-data-class.kt |
| 253 | + :language: kotlin |
| 254 | + :start-after: start-agg-data-class |
| 255 | + :end-before: end-agg-data-class |
| 256 | + :dedent: |
| 257 | + |
| 258 | +API Documentation |
| 259 | +----------------- |
| 260 | + |
| 261 | +- `{+driver-short+} Extensions |
| 262 | + <{+core-api+}/apidocs/mongodb-driver-kotlin-extensions/index.html>`__ |
0 commit comments