Docs Menu

Index a Field - Kotlin SDK

Indexes speed up some queries, but slow down inserts and updates. Realm Database stores indexes on disk, which makes your realm files larger. To index a field, use the @Index annotation:

class Movie: RealmObject {
val _id: Long = Random.nextLong()
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
←  Ignore a Field - Kotlin SDKOptional Fields - Kotlin SDK →
Give Feedback
© 2022 MongoDB, Inc.


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