MongoDB Realm Kotlin SDK (Beta)
On this page
This SDK is currently offered as a beta release. We encourage you to try out the feature and give feedback. However, be aware that APIs and functionality are subject to change.
The MongoDB Realm Kotlin SDK allows you to use Realm Database from:
- Android and iOS applications written with Kotlin Multiplatform Mobile (KMM)
- standalone Android applications
The SDK currently supports some, but not all, Realm Sync functionality. For more information about supported behavior, see the API reference.
Local Realm Database
With the MongoDB Realm Kotlin SDK, you can access objects stored in a local instance of Realm Database. With Realm Database, you can:
Define an Object Schema
Define your object schema with marked Kotlin classes:
class Frog : RealmObject { var name: String = "" var age: Int = 0 var species: String? = null var owner: String? = null }
Query Realm Database
Query for stored objects:
val config = RealmConfiguration.Builder(schema = setOf(Frog::class)) .build() val realm = Realm.open(config) val tadpoles: RealmQuery<Frog> = realm.query<Frog>("age > $0", 2) Log.v("Tadpoles: ${tadpoles.count()}") val numTadpolesNamedJasonFunderburker = tadpoles.query("name == $0", "Jason Funderburker").count() Log.v("Tadpoles named Jason Funderburker: $numTadpolesNamedJasonFunderburker")
Update Objects
Update objects in Realm Database by updating field values on an instance of the object within a transaction:
val config = RealmConfiguration.Builder(schema = setOf(Frog::class)) .build() val realm = Realm.open(config) // start a write transaction realm.writeBlocking { // get a frog from the database to update val frog: Frog? = query<Frog>() .query("name == $0 LIMIT(1)", "Benjamin Franklin") .first() .find() // update the frog's properties frog?.apply { name = "George Washington" species = "American bullfrog" } } // when the transaction completes, the frog's name and species // are updated in the database
Get Started
To start using the MongoDB Realm Kotlin SDK in your application, see the installation guide for Kotlin Multiplatform or Android. Once you've installed the SDK, check out the Quick Start.