Docs Menu

Modify an Object - Kotlin SDK

To modify an object stored within a realm:

  1. Open a write transaction with realm.write() or realm.writeBlocking().
  2. Query the transaction's mutable realm with realm.query(). Specify the object type as a type parameter passed to query(). To ensure your query returns the correct object, filter with unique identifying information such as a primary key value.
  3. Change an object property within the write transaction. The SDK automatically persists changes to the realm.
realm.write {
// fetch a frog from the realm by primary key
val frog: Frog? =
this.query<Frog>("_id == $0", PRIMARY_KEY_VALUE).first().find()
// modify the frog's age in the write transaction to persist the new age to the realm
frog?.age = 42
}
Note

You can only modify objects in a realm within a write transaction.

←  Update - Kotlin SDKUpsert an Object - Kotlin SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.