@@ -159,6 +159,10 @@ operations.
159
159
using infix notation:
160
160
161
161
.. code-block:: kotlin
162
+
163
+ import com.mongodb.kotlin.client.model.Filters.eq
164
+ import com.mongodb.kotlin.client.model.Filters.lt
165
+ import com.mongodb.kotlin.client.model.Updates.set
162
166
163
167
data class Jedi(val name: String, val age: Int)
164
168
@@ -239,20 +243,20 @@ Both drivers provide support for type-safe queries using property references.
239
243
240
244
.. code-block:: kotlin
241
245
242
- data class Person(val name: String, val email: String, val gender: String, val age: Int)
243
- data class Results(val email: String)
244
-
245
- val collection = database.getCollection<Person>("people")
246
-
247
- // Using Builders
248
- val filter = and(eq("gender", "female"), gt("age", 29))
249
- val projection = fields(excludeId(), include("email"))
250
- val results = collection.find<Results>(filter).projection(projection)
251
-
252
- // Using Document class
253
- val filter = Document().append("gender", "female").append("age", Document().append("\$gt", 29))
254
- val projection = Document().append("_id", 0).append("email", 1)
255
- val results = collection.find<Results>(filter).projection(projection)
246
+ data class Person(val name: String, val email: String, val gender: String, val age: Int)
247
+ data class Results(val email: String)
248
+
249
+ val collection = database.getCollection<Person>("people")
250
+
251
+ // Using Builders
252
+ val filter = and(eq("gender", "female"), gt("age", 29))
253
+ val projection = fields(excludeId(), include("email"))
254
+ val results = collection.find<Results>(filter).projection(projection)
255
+
256
+ // Using Document class
257
+ val filter = Document().append("gender", "female").append("age", Document().append("\$gt", 29))
258
+ val projection = Document().append("_id", 0).append("email", 1)
259
+ val results = collection.find<Results>(filter).projection(projection)
256
260
257
261
To map a KMongo string query to the {+driver-short+}, you can use the ``JsonObject`` class.
258
262
@@ -277,17 +281,23 @@ Both drivers provide support for type-safe queries using property references.
277
281
278
282
.. code-block:: kotlin
279
283
280
- data class Person(val name: String, val gender: String, val age: Int)
281
- data class Result(val name: String)
284
+ import com.mongodb.kotlin.client.model.Filters.eq
285
+ import com.mongodb.kotlin.client.model.Filters.and
286
+ import com.mongodb.kotlin.client.model.Filters.gt
287
+ import com.mongodb.kotlin.client.model.Projections.excludeId
288
+ import com.mongodb.kotlin.client.model.Projections.fields
289
+ import com.mongodb.kotlin.client.model.Projections.include
282
290
283
- val collection = database.getCollection<Person>("people")
291
+ data class Person(val name: String, val gender: String, val age: Int)
292
+ data class Result(val name: String)
284
293
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
-
294
+ val collection = database.getCollection<Person>("people")
295
+
296
+ // Infix Notation Query
297
+ val filter = (Person::gender eq "female") and (Person::age gt 29))
298
+ val projection = fields(excludeId(), include(Person::name))
299
+
300
+ val results = collection.find<Result>(filter).projection(projection)
291
301
292
302
To learn more and view examples that use all of the builder
293
303
classes, see the :ref:`kotlin-builders-data-classes` guide.
@@ -500,7 +510,7 @@ Both drivers support synchronous and asynchronous operations.
500
510
val collection = database.getCollection<Jedi>("jedi")
501
511
502
512
// Synchronous operations
503
- val jedi =a Jedi("Luke Skywalker", 19)
513
+ val jedi = Jedi("Luke Skywalker", 19)
504
514
collection.insertOne(jedi)
505
515
506
516
To write asynchronous coroutine code:
@@ -519,7 +529,7 @@ Both drivers support synchronous and asynchronous operations.
519
529
runBlocking {
520
530
521
531
// Async operations
522
- val jedi =a Jedi("Luke Skywalker", 19)
532
+ val jedi = Jedi("Luke Skywalker", 19)
523
533
collection.insertOne(jedi)
524
534
}
525
535
0 commit comments