Docs Menu

Docs HomeRealm

Create a New Object - Kotlin SDK

Instantiate Realm objects as you would any other object. In a transaction, you can add the object to the realm if the realm's schema includes the object type. When you add an instance to the realm, it becomes managed by that realm.

To persist a new object to a realm:

  1. Instantiate a new object instance with the class constructor. You can use an apply block to configure multiple properties at once.

  2. Open a write transaction with realm.write() or realm.writeBlocking().

  3. Pass the new object instance to copyToRealm() to persist the object data to the realm. This method returns a managed instance of the object. You can modify the persisted object through the returned instance.

realm.write {
this.copyToRealm(Frog().apply {
name = "Kermit"
age = 45
species = "Green"
owner = "Jim"


You can only insert new objects into a realm within a write transaction.

←  Create - Kotlin SDKRead - Kotlin SDK →
Share Feedback
© 2022 MongoDB, Inc.


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