Docs Menu

MongoDB Realm Kotlin Multiplatform SDK (Alpha)

On this page

  • Local Realm Database
  • Define an Object Schema
  • Query Realm Database
  • Update Objects
  • Get Started
Warning
Alpha Release

This SDK is currently offered as an alpha 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 Multiplatform SDK allows you to use Realm Database from Android and iOS applications written with Kotlin Multiplatform Mobile (KMM).

Note
Local Realm Database Only

The Kotlin Multiplatform SDK currently only supports local Realm Database. You cannot use the SDK to connect to MongoDB Realm backend apps.

With the MongoDB Realm Kotlin Multiplatform SDK, you can access objects stored in a local instance of Realm Database. With Realm Database, you can:

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 for stored objects:

val config = RealmConfiguration(schema = setOf(Frog::class))
val realm = Realm(config)
val frogsQuery = realm.objects<Frog>()
val numTadpoles = frogsQuery.query("age > $0", 2).count()
print("Tadpoles: $numTadpoles")
val numFrogsNamedJasonFunderburker = frogsQuery.query("name == $0", "Jason Funderburker").count()
print("Frogs named Jason Funderburker: $numFrogsNamedJasonFunderburker")
val numFrogsWithoutOwners = frogsQuery.query("owner == null").count()
print("Frogs without owners: $numFrogsWithoutOwners")

Update objects in Realm Database by updating field values on an instance of the object within a transaction:

val config = RealmConfiguration(schema = setOf(Frog::class))
val realm = Realm(config)
// start a write transaction
realm.writeBlocking {
// get a frog from the database to update
val frog = this.objects<Frog>()
.query("name == $0 LIMIT(1)", "Benjamin Franklin")
// change the frog's name
frog[0].name = "George Washington"
// change the frog's species
frog[0].species = "American bullfrog"
} // when the transaction completes, the frog's name and species
// are updated in the database

To start using the MongoDB Realm Kotlin Multiplatform SDK in your KMM application, see Install Realm for Kotlin Multiplatform to add the Kotlin Multiplatform SDK dependency and then check out the Quick Start.

Important
Kotlin Multiplatform SDK is Currently in Alpha

The Kotlin Multiplatform SDK, like KMM itself, is currently in alpha. The API may change in future releases.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.