4
4
Migrate from KMongo
5
5
===================
6
6
7
+ .. facet::
8
+ :name: genre
9
+ :values: tutorial
10
+
11
+ .. meta::
12
+ :description: Learn how to migrate your application from KMongo to the official MongoDB Kotlin driver.
13
+ :keywords: code example, adapt, syntax
14
+
7
15
.. contents:: On this page
8
16
:local:
9
17
:backlinks: none
@@ -45,7 +53,7 @@ Both drivers let you connect to and communicate with MongoDB clusters from a
45
53
46
54
import com.mongodb.kotlin.client.coroutine.MongoClient
47
55
48
- data class Jedi(val name: String, val age: Int)
56
+ data class Jedi(val name: String, val age: Int)
49
57
50
58
// Replace the placeholder with your MongoDB deployment's connection string
51
59
val uri = CONNECTION_STRING_URI_PLACEHOLDER
@@ -87,6 +95,15 @@ CRUD and Aggregation
87
95
Both drivers provide support for all MongoDB CRUD APIs and aggregation
88
96
operations.
89
97
98
+ .. tip::
99
+
100
+ If you are accustomed to constructing query filters by using the
101
+ infix notation available in KMongo, you can also use this notation to
102
+ create filters in the official {+driver-short+} by using extension
103
+ methods from the ``mongodb-driver-kotlin-extensions`` package. Select
104
+ the :guilabel:`Kotlin Driver Extensions` tab to view an example that
105
+ uses this query syntax in the {+driver-short+}.
106
+
90
107
.. tabs::
91
108
92
109
.. tab::
@@ -96,42 +113,71 @@ operations.
96
113
97
114
.. code-block:: kotlin
98
115
99
- // Insert a document
100
- val jedi = Jedi("Luke Skywalker", 19)
101
- collection.insertOne(jedi)
116
+ // Insert a document
117
+ val jedi = Jedi("Luke Skywalker", 19)
118
+ collection.insertOne(jedi)
102
119
103
- // Find a document
104
- val luke = collection.find(Jedi::name.name, "Luke Skywalker")
105
- val jedis = collection.find(lt(Jedi::age.name, 30)).toList()
120
+ // Find a document
121
+ val luke = collection.find(Jedi::name.name, "Luke Skywalker")
122
+ val jedis = collection.find(lt(Jedi::age.name, 30)).toList()
106
123
107
- // Update a document
108
- val filter = Filters.eq(Jedi::name.name, "Luke Skywalker")
109
- val update = Updates.set(Jedi::age.name, 20)
110
- collection.updateOne(filter, update)
124
+ // Update a document
125
+ val filter = Filters.eq(Jedi::name.name, "Luke Skywalker")
126
+ val update = Updates.set(Jedi::age.name, 20)
127
+ collection.updateOne(filter, update)
111
128
112
- // Delete a document
113
- val filter = Filters.eq(Jedi::name.name, "Luke Skywalker")
114
- collection.deleteOne(filter)
129
+ // Delete a document
130
+ val filter = Filters.eq(Jedi::name.name, "Luke Skywalker")
131
+ collection.deleteOne(filter)
115
132
116
- Aggregation pipelines can be built using the ``aggregate`` method and the
117
- ``pipeline`` function:
133
+ You can build aggregation pipelines by using the ``aggregate()``
134
+ method and the ``pipeline`` function:
118
135
119
136
.. code-block:: kotlin
120
137
121
- data class Results(val avgAge: Double)
138
+ data class Results(val avgAge: Double)
122
139
123
- val resultsFlow = collection.aggregate<Results>(
124
- listOf(
125
- Aggregates.match(Filters.ne(Jedi::name.name, "Luke Skywalker")),
126
- Aggregates.group("\$${Jedi::name.name}",
127
- Accumulators.avg("avgAge", "\$${Jedi::age.name}"))
128
- )
129
- )
130
- resultsFlow.collect { println(it) }
140
+ val resultsFlow = collection.aggregate<Results>(
141
+ listOf(
142
+ Aggregates.match(Filters.ne(Jedi::name.name, "Luke Skywalker")),
143
+ Aggregates.group("\$${Jedi::name.name}",
144
+ Accumulators.avg("avgAge", "\$${Jedi::age.name}"))
145
+ )
146
+ )
147
+ resultsFlow.collect { println(it) }
131
148
132
149
See the :ref:`CRUD Operations <kotlin-crud-operations>` and
133
150
:ref:`Aggregation <kotlin-aggregation>` documentation for more information.
134
-
151
+
152
+ .. tab::
153
+ :tabid: Kotlin Driver Extensions
154
+
155
+ You can use the Builders API from the
156
+ ``mongodb-driver-kotlin-extensions`` library to create query
157
+ filters and aggregation pipeline stages directly using data class
158
+ properties. This library also allows you to create queries by
159
+ using infix notation:
160
+
161
+ .. code-block:: kotlin
162
+
163
+ data class Jedi(val name: String, val age: Int)
164
+
165
+ // Find documents
166
+ val luke = collection.find(Jedi::name eq "Luke Skywalker")
167
+ val jedis = collection.find(Jedi::age lt 30)).toList()
168
+
169
+ // Update a document
170
+ val filter = Jedi::name eq "Luke Skywalker"
171
+ val update = Jedi::age.name set 20
172
+ collection.updateOne(filter, update)
173
+
174
+ // Delete a document
175
+ val filter = Jedi::name eq "Luke Skywalker"
176
+ collection.deleteOne(filter)
177
+
178
+ To learn more and view examples that use all of the builder
179
+ classes, see the :ref:`kotlin-builders-data-classes` guide.
180
+
135
181
.. tab::
136
182
:tabid: KMongo
137
183
@@ -169,19 +215,19 @@ operations.
169
215
`Extensions Overview <https://litote.org/kmongo/extensions-overview/>`__ KMongo
170
216
documentation.
171
217
218
+ Construct Queries
219
+ -----------------
220
+
221
+ Both drivers provide support for type-safe queries using property references.
222
+
172
223
.. tip::
173
224
174
225
If you are accustomed to constructing query filters by using the
175
226
infix notation available in KMongo, you can also use this notation to
176
227
create filters in the official {+driver-short+} by using extension
177
- methods from the ``mongodb-driver-kotlin-extensions`` package. To
178
- learn more and view examples, see the
179
- :ref:`kotlin-builders-data-classes` guide.
180
-
181
- Construct Queries
182
- -----------------
183
-
184
- Both drivers provide support for type-safe queries using property references.
228
+ methods from the ``mongodb-driver-kotlin-extensions`` package. Select
229
+ the :guilabel:`Kotlin Driver Extensions` tab to view an example that
230
+ uses this query syntax in the {+driver-short+}.
185
231
186
232
.. tabs::
187
233
@@ -220,7 +266,32 @@ Both drivers provide support for type-safe queries using property references.
220
266
- :ref:`Builders <kotlin-builders-landing>`
221
267
- :ref:`Documents <kotlin-document-format>` guide
222
268
- `JsonObject <{+api+}/apidocs/bson/org/bson/json/JsonObject.html>`__ API Documentation
223
-
269
+
270
+ .. tab::
271
+ :tabid: Kotlin Driver Extensions
272
+
273
+ You can use the Builders API from the
274
+ ``mongodb-driver-kotlin-extensions`` library to construct queries
275
+ directly on data class properties. This library also allows you to
276
+ create queries by using infix notation:
277
+
278
+ .. code-block:: kotlin
279
+
280
+ data class Person(val name: String, val gender: String, val age: Int)
281
+ data class Result(val name: String)
282
+
283
+ val collection = database.getCollection<Person>("people")
284
+
285
+ // Infix Notation Query
286
+ val filter = (Person::gender eq "female") and (Person::age gt 29))
287
+ val projection = fields(excludeId(), include(Person::name))
288
+
289
+ val results = collection.find<Result>(filter).projection(projection)
290
+
291
+
292
+ To learn more and view examples that use all of the builder
293
+ classes, see the :ref:`kotlin-builders-data-classes` guide.
294
+
224
295
.. tab::
225
296
:tabid: KMongo
226
297
@@ -260,15 +331,6 @@ Both drivers provide support for type-safe queries using property references.
260
331
- `Typed Queries <https://litote.org/kmongo/typed-queries/>`_
261
332
- `Mongo Shell Queries <https://litote.org/kmongo/mongo-shell-support/>`__
262
333
263
- .. tip::
264
-
265
- If you are accustomed to constructing query filters by using the
266
- infix notation available in KMongo, you can also use this notation to
267
- create filters in the official {+driver-short+} by using extension
268
- methods from the ``mongodb-driver-kotlin-extensions`` package. To
269
- learn more and view examples, see the
270
- :ref:`kotlin-builders-data-classes` guide.
271
-
272
334
Data Typing
273
335
-----------
274
336
0 commit comments