Docs Menu

Docs HomeRealm

Index a Field - Kotlin SDK

Indexes speed up some queries at the cost of slightly slower write times and additional storage and memory overhead. Realm Database stores indexes on disk, which makes your realm files larger. Each index entry is a minimum of 12 bytes.

To index a field, use the @Index annotation:

class Movie : RealmObject {
@Index
val _id: ObjectId = ObjectId()
val starring: List<String> = listOf()
}

Indexes support the efficient execution of queries in Realm Database. Without indexes, Realm Database must perform a collection scan, i.e., scan every document in a collection, to select those documents that match a query. If an appropriate index exists for a query, Realm Database can use the index to limit the number of documents that it must inspect.

Indexes are special data structures that store a small portion of a realm's data in an easy to traverse form. The index stores the value of a specific field ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations.

You can index fields with the following types:

  • String

  • Byte

  • Short

  • Int

  • Long

  • Boolean

  • RealmInstant

  • ObjectId

  • RealmUUID

←  Ignore a Field - Kotlin SDKOptional Fields - Kotlin SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

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