A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/fundamentals/builders/ below:

Builders - Kotlin Coroutine - MongoDB Docs

This section includes guides on how to use each of the available builders and demonstrates the utility that the Kotlin driver builder classes provide.

The Kotlin driver provides classes to simplify the process of performing CRUD operations and using the Aggregation API. The static utility methods allow you to build queries and other kinds of documents more efficiently.

When you use builders classes, you leverage the following products:

When using builders, the Kotlin compiler and the IDE catch errors such as misspelled operators or missing parameters early on. If you use the MongoDB shell or plain Kotlin instead, you write operators as strings and get no visual indication of a problem, which pushes these errors to runtime instead of compile time.

By using builder classes, you can write operators as methods, so that your IDE instantly indicates whether your code has errors. While developing, your IDE can also show you methods that you can use and can complete your code with placeholder parameters.

Suppose you want to send a marketing email to all users in the users collection that meet the following criteria:

You also need your query to return only their email addresses.

The documents in the users collection are modeled by the following data class:

data class User(    @BsonId    val id: BsonObjectId = BsonObjectId(),    val gender: String,    val age: Int,    val email: String,)

The following code provides the command you use in the MongoDB Shell to perform the query:

collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 })

The following code provides the find operation you create without builders in the Kotlin driver:

data class Results(val email: String)val filter = Document().append("gender", "female").append("age", Document().append("\$gt", 29))val projection = Document().append("_id", 0).append("email", 1)val results = collection.find<Results>(filter).projection(projection)

In this case, you might easily include an error when writing the "\$gt" operator in the filter, but your IDE returns the relevant error only at runtime.

The following code provides the find operation you create by using builders in the Kotlin driver:

import com.mongodb.client.model.Filtersimport com.mongodb.client.model.Projections
data class Results(val email: String)val filter = Filters.and(Filters.eq(User::gender.name, "female"), Filters.gt(User::age.name, 29))val projection = Projections.fields(Projections.excludeId(), Projections.include("email"))val results = collection.find<Results>(filter).projection(projection)

The Use Builders with Data Classes guide provides examples on how to use the preceding builders classes directly with data class properties. This guide might help to make your application more type-safe and improve Kotlin interoperability.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4