44Migrate from KMongo
55===================
66
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+
715.. contents:: On this page
816 :local:
917 :backlinks: none
@@ -45,7 +53,7 @@ Both drivers let you connect to and communicate with MongoDB clusters from a
4553
4654 import com.mongodb.kotlin.client.coroutine.MongoClient
4755
48- data class Jedi(val name: String, val age: Int)
56+ data class Jedi(val name: String, val age: Int)
4957
5058 // Replace the placeholder with your MongoDB deployment's connection string
5159 val uri = CONNECTION_STRING_URI_PLACEHOLDER
@@ -87,6 +95,15 @@ CRUD and Aggregation
8795Both drivers provide support for all MongoDB CRUD APIs and aggregation
8896operations.
8997
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+
90107.. tabs::
91108
92109 .. tab::
@@ -96,42 +113,71 @@ operations.
96113
97114 .. code-block:: kotlin
98115
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)
102119
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()
106123
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)
111128
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)
115132
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:
118135
119136 .. code-block:: kotlin
120137
121- data class Results(val avgAge: Double)
138+ data class Results(val avgAge: Double)
122139
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) }
131148
132149 See the :ref:`CRUD Operations <kotlin-crud-operations>` and
133150 :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+
135181 .. tab::
136182 :tabid: KMongo
137183
@@ -169,19 +215,19 @@ operations.
169215 `Extensions Overview <https://litote.org/kmongo/extensions-overview/>`__ KMongo
170216 documentation.
171217
218+ Construct Queries
219+ -----------------
220+
221+ Both drivers provide support for type-safe queries using property references.
222+
172223.. tip::
173224
174225 If you are accustomed to constructing query filters by using the
175226 infix notation available in KMongo, you can also use this notation to
176227 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+}.
185231
186232.. tabs::
187233
@@ -220,7 +266,32 @@ Both drivers provide support for type-safe queries using property references.
220266 - :ref:`Builders <kotlin-builders-landing>`
221267 - :ref:`Documents <kotlin-document-format>` guide
222268 - `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+
224295 .. tab::
225296 :tabid: KMongo
226297
@@ -260,15 +331,6 @@ Both drivers provide support for type-safe queries using property references.
260331 - `Typed Queries <https://litote.org/kmongo/typed-queries/>`_
261332 - `Mongo Shell Queries <https://litote.org/kmongo/mongo-shell-support/>`__
262333
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-
272334Data Typing
273335-----------
274336
0 commit comments