A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/realm-database/schemas/supported-types/ below:

Supported Types - Kotlin SDK - Atlas Device SDKs

This page describes the supported data types that you can use to define properties in your object model. For more information on how to define your object model, refer to Define an Object Model.

To learn how specific data types are mapped to BSON types in an App Services Schema, refer to Data Model Mapping in the Atlas App Services documentation.

The Kotlin SDK supports the following Kotlin types , BSON types, and Realm-specific types, which you can use for unique identifiers, timestamps, counters, and collections.

The Kotlin SDK does not natively support:

Realm object properties must be mutable and initialized when declared. The Kotlin SDK does not currently support abstract properties. You can declare properties optional (nullable) using the built-in ? Kotlin operator, or you can assign a default value to a property when you declare it.

Note

Realm stores all non-decimal numeric types as Long values and all decimal numeric types as Double values.

Kotlin Data Types

The following table lists the supported Kotlin data types and examples of how to declare them as required or optional properties in your object model.

Kotlin Data Type

Required

Optional

String

var stringReq: String = ""
var stringOpt: String? = null

Byte

var byteReq: Byte = 0
var byteOpt: Byte? = null

Short

var shortReq: Short = 0
var shortOpt: Short? = null

Int

var intReq: Int = 0
var intOpt: Int? = null

Long

var longReq: Long = 0L
var longOpt: Long? = null

Float

var floatReq: Float = 0.0f
var floatOpt: Float? = null

Double

var doubleReq: Double = 0.0
var doubleOpt: Double? = null

Boolean

var boolReq: Boolean = false
var boolOpt: Boolean? = null

Char

var charReq: Char = 'a'
var charOpt: Char? = null

MongoDB BSON Types

The following table lists the supported MongoDB BSON data types and examples of how to declare them as required or optional properties in your object model. To use these types, you must import them from the org.mongodb.kbson package.

MongoDB BSON Type

Required

Optional

ObjectId

var objectIdReq: ObjectId = ObjectId()
var objectIdOpt: ObjectId? = null

Decimal128

var decimal128Req: Decimal128 = Decimal128("123.456")
var decimal128Opt: Decimal128? = null

Realm-Specific Types

The following table lists the supported Realm-specific data types and examples of how to declare them as required or optional properties in your object model.

Realm-Specific Type

Required

Optional

RealmUUID

var uuidReq: RealmUUID = RealmUUID.random()
var uuidOpt: RealmUUID? = null

RealmInstant

var realmInstantReq: RealmInstant = RealmInstant.now()
var realmInstantOpt: RealmInstant? = null

RealmAny

N/A

var realmAnyOpt: RealmAny? = RealmAny.create("foo")

MutableRealmInt

var mutableRealmIntReq: MutableRealmInt = MutableRealmInt.create(0)
var mutableRealmIntOpt: MutableRealmInt? = null

RealmList

var listReq: RealmList<CustomObjectType> = realmListOf()

N/A

RealmSet

var setReq: RealmSet<String> = realmSetOf()

N/A

RealmDictionary

var dictionaryReq: RealmDictionary<String> = realmDictionaryOf()

N/A

RealmObject

N/A

var realmObjectPropertyOpt: CustomObjectType? = null

EmbeddedRealmObject

N/A

var embeddedProperty: EmbeddedObjectType? = null

The Kotlin SDK supports UUID and ObjectId as unique identifiers for Realm objects.

Note Using UUID Instead of ObjectId

In general, you can use UUID for any fields that function as a unique identifier. Using UUID might be particularly useful if you are migrating data not stored in MongoDB since it is likely that your object's unique identifiers are already of a UUID type. Alternatively, using ObjectId might be useful for a collection of data that already exists in MongoDB.

ObjectId is a MongoDB-specific BSON type. It is a 12-byte, globally unique value that you can use as an identifier for objects. It is nullable, indexable, and can be used as a primary key.

You can initialize an ObjectId using ObjectId().

UUID (Universal Unique Identifier) is a 16-byte unique value that you can use as an identifier for objects. It is nullable, indexable, and can be used as a primary key.

Realm creates UUIDs with the RealmUUID type that conform to RFC 4122 version 4 and are created with random bytes.

You can generate a random RealmUUID using RealmUUID.random() or pass a UUID-formatted string to RealmUUID.from():

val uuid1 = RealmUUID.from("46423f1b-ce3e-4a7e-812f-004cf9c42d76")val uuid2 = RealmUUID.random()

The Kotlin SDK offers MutableRealmInt as a special integer type that you can use as a logical counter to accurately synchronize numeric changes across multiple distributed clients. It behaves like a Long but also supports increment and decrement methods that implement a conflict-free replicated data type . This ensures that numeric updates can be executed regardless of order to converge to the same value.

A MutableRealmInt property:

Additionally, MutableRealmInt fields:

Learn how to Create a MutableRealmInt (Counter) Property and Update a MutableRealmInt (Counter) Property.

You cannot store Kotlin's built-in Date or Instant types in Realm.

Instead, the Kotlin SDK uses the RealmInstant type to store time information as a Unix epoch timestamp.

If you need timestamp data in a form other than RealmInstant, you can add conversion code to your model class based on the following example:

class RealmInstantConversion : RealmObject {    private var _timestamp: RealmInstant = RealmInstant.from(0, 0)    public var timestamp: Instant        get() {            return _timestamp.toInstant()        }        set(value) {            _timestamp = value.toRealmInstant()        }}fun RealmInstant.toInstant(): Instant {    val sec: Long = this.epochSeconds            val nano: Int = this.nanosecondsOfSecond    return if (sec >= 0) {         Instant.fromEpochSeconds(sec, nano.toLong())    } else {                                Instant.fromEpochSeconds(sec - 1, 1_000_000 + nano.toLong())    }}fun Instant.toRealmInstant(): RealmInstant {    val sec: Long = this.epochSeconds        val nano: Int = this.nanosecondsOfSecond    return if (sec >= 0) {         RealmInstant.from(sec, nano)    } else {                                RealmInstant.from(sec + 1, -1_000_000 + nano)    }}

Changed in version 2.0.0: RealmAny can hold lists and dictionaries of mixed data.

RealmAny represents a non-nullable mixed data type. It behaves like the value type that it contains. RealmAny can hold:

RealmAny cannot hold EmbeddedRealmObject types, RealmSet, or another RealmAny.

RealmAny properties:

You can store multiple RealmAny instances in RealmList, RealmDictionary, or RealmSet fields.

Tip Handle Polymorphism with Conditional Expressions

Because you must know the stored type to extract its value, we recommend using a when expression to handle the RealmAny type and its possible inner value class.

In version 2.0.0 and later, a RealmAny data type can hold collections (a list or dictionary, but not a set) of RealmAny elements. You can use mixed collections to model unstructured or variable data. For more information, refer to Define Unstructured Data.

To use mixed collections in your app, define the mixed type property in your data model the same way you would any other RealmAny type. Then, create the list or dictionary collections using RealmAny.create().

The Kotlin SDK offers several collection types that you can use as properties in your data model. A collection is an object that contains zero or more instances of one supported data type. Realm collections are homogenous (all objects in a collection are of the same type) and are backed by their corresponding built-in Kotlin classes.

Collection types are non-null. When you define a collection property, you must initialize it. For more information, refer to Create a Collection.

The RealmList type implements Kotlin's List interface. Unmanaged lists behave like Kotlin's MutableList.

A RealmList represents a to-many relationship containing:

RealmList<E> is a non-null type, where:

The RealmSet type implements Kotlin's Set interface. Unmanaged sets behave like Kotlin's MutableSet.

A RealmSet represents a to-many relationship containing distinct values of:

You cannot use EmbeddedRealmObject elements in a RealmSet.

RealmSet<E> is a non-null type, where:

The RealmMap type implements Kotlin's Map interface and is an associative array that contains key-value String pairs with unique keys. RealmDictionary is a specialized RealmMap that accepts a String key and non-string values. Unmanaged dictionaries behave like Kotlin's LinkedHashMap.

RealmDictionary values can be:

RealmDictionary<K, V> is a non-null type, where:

You can use RealmObject and any subclasses, except AsymmetricRealmObject as properties in your object model.

Important

AsymmetricRealmObject cannot be used as properties. For more information, refer to Asymmetric Objects.

A RealmObject type represents a custom object that you can use as a property.

RealmObject properties:

You can also reference one or more Realm objects from another through to-one and to-many relationships. For more information, refer to the Relationships page.

A backlink represents an inverse, to-many relationship between a RealmObject and one or more RealmObject or between a RealmObject and an EmbeddedRealmObject. Backlinks cannot be null.

Backlinks implement:

For more information, refer to Inverse Relationships.

An EmbeddedRealmObject is a special type of RealmObject.

EmbeddedRealmObject properties:

For more information, refer to Embedded Objects.

New in version 1.11.0.

The Kotlin SDK supports geospatial queries using the following data types:

Important Cannot Persist Geospatial Data Types

Currently, geospatial data types cannot be persisted. For example, you can't declare a property that is of type GeoBox.

These types can only be used as arguments for geospatial queries.

For more information on querying with geospatial data, refer to Geospatial Data.

The Kotlin SDK does not natively support enumerations, or enums. To use enums in a Realm object class, define a field with a type matching the underlying data type of your enum.

Then, create getters and setters for the field that convert the field value between the underlying value and the enum type.

enum class EnumClass(var state: String) {    NOT_STARTED("NOT_STARTED"),    IN_PROGRESS("IN_PROGRESS"),    COMPLETE("COMPLETE")}class EnumObject : RealmObject {    var name: String? = null    private var state: String = EnumClass.NOT_STARTED.state    var stateEnum: EnumClass        get() = EnumClass.valueOf(state)        set(value) {            state = value.state        }}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4