Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Datos del modelo

Model Data with Device Sync - Kotlin SDK

This page describes the Device Sync data model and how it's used to map data from the App Services schema used by Device Sync to the Realm schema used by the Kotlin SDK in the client.

This page does not:

Para obtener una explicación detallada de la sincronización del dispositivo, consulte Comience a utilizar Atlas Device Sync en la documentación de Atlas App Services.

Tip

The table in the Schema Types Mapping List section on this page provides a quick reference for supported data types and how they map between Realm and App Services. For a more comprehensive resource, refer to Data Model Mapping in the App Services documentation.

The Atlas Device Sync data model is defined by the following schemas, which allows Device Sync to map your data between the client and Atlas:

  • Esquema Realm: el Modelo de objeto del lado del cliente en tu aplicación que define tus datos como clases de Kotlin usando el Kotlin SDK.

  • App Services schema: the server-side schema in Atlas App Services that defines your data in BSON. For more information, refer to Schemas in the App Services documentation.

Device Sync utiliza estos esquemas para validar y convertir objetos entre el formato Kotlin y BSON al sincronizar datos. Cuando sincronizas datos desde el cliente, Device Sync convierte automáticamente los tipos de datos de Realm Kotlin a BSON. Luego, cuando el dispositivo cliente sincroniza datos de Atlas a través de Device Sync, el SDK convierte los datos BSON nuevamente en objetos Kotlin.

To use Device Sync, you must define a Realm schema and an App Services schema, and both schemas must be consistent with each other.

You can define your schema in the client app or in Atlas first, depending on your preference and use case. You can then generate a corresponding schema with matching object models.

If you are developing a new client application, you likely want to iterate on the data model in the client app. After you define an object model directly in your client app code, you can enable Development Mode in App Services to generate a matching App Services schema automatically.

Modo de desarrollo es una configuración que permite que Device Sync infiera y actualice esquemas a partir de modelos de datos del lado del cliente cuando sincronizas datos desde el cliente. Para obtener más información, consulta Modo de desarrollo en la documentación de App Services.

If you are developing a client application that works with data that already exists in Atlas, you can generate a schema from that data, and then generate SDK object models to use in your Kotlin client app. To learn more, refer to Sync Data in Atlas with a Client Application in the App Services documentation.

There are a few requirements to successfully sync objects. As outlined earlier, you must have matching Realm and App Services schemas that contain the objects that you want to sync.

Además, Device Sync requiere que:

  • Sus modelos de objetos deben tener un campo de clave principal llamado _idPuede ser de tipo String, Int o ObjectId. Device Sync lo utiliza para identificar objetos en las colecciones de Atlas.

    Si un objeto no tiene definido un campo _id, Realm mostrará el siguiente error de validación de esquema: There must be a primary key property named '_id' on a synchronized Realm.

  • Cada clase debe tener al menos un campos consultables.

    Cuando el modo de desarrollo está habilitado, los campos que se incluyen en las consultas de suscripción de cliente se agregan automáticamente como campos consultables en Atlas. Para obtener más información sobre la configuración de suscripciones con el SDK de Kotlin, consulte la sección "Descripción general de suscripciones".

Los objetos de Realm son instancias con nombres únicos de clases Kotlin definidas en su esquema de Realm que determinan las propiedades y relaciones de los objetos de ese tipo.

App Services maps Realm objects to Atlas in the following ways:

  • Realm object names map to Atlas collections in your linked Device Sync data source. Note the following:

    • Cuando el modo de desarrollo está habilitado, App Services crea automáticamente una colección y esquema para cada nuevo tipo de objeto Realm que sincronices.

    • Embedded objects are not stored in their own collection in Atlas. This is because they cannot exist outside of their parent object type. Refer to the Embedded Objects section on this page for more information.

  • The Realm object schema maps to an App Services schema within its appropriate collection.

En el siguiente ejemplo, tenemos objetos Frog y Pond con propiedades básicas definidas en nuestro esquema de Realm:

Frog and Pond Realm Objects
// Maps to `Frog` collection
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int? = null
}
// Maps to `Pond` collection
class Pond : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
}

En Atlas, podemos ver cómo estos tipos de objetos se mapean a los respectivos esquemas de App Services -- cada objeto se asocia con su respectiva colección. También vemos ejemplos de objetos rana y estanque que se ajustan a este modelo de datos:

Frog collection in App Services
{
"title": "Frog",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"age": {
"bsonType": "long"
},
"name": {
"bsonType": "string"
}
}
}
Pond collection in App Services
{
"title": "Pond",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"name": {
"bsonType": "string"
}
}
}
Ejemplo de objeto Frog en Atlas
{
"_id": ObjectId("5af712eff26b29dc5c51c60f"),
"name": "Kermit",
"age": 42
}
Ejemplo de objeto Pond en Atlas
{
"_id": ObjectId("5af714eff24b294c5251cf04"),
"name": "Kermit's Pond"
}

Your Realm object model might include relationships between objects. There are two primary types of relationships:

  • To-one relationship: an object is related in a specific way to no more than one other Realm object.

  • To-many relationship: an object is related in a specific way to multiple Realm objects.

Las relaciones se mapean por propiedades que hacen referencia a la llave primariade otro objeto Realm.

Para obtener más información sobre la modelización de relaciones en un esquema de Servicios de aplicación, consulta Relaciones en la documentación de Servicios de aplicación.

Una relación uno a uno (to-one relationship) asigna una propiedad a una sola instancia de otro objeto Realm. Las relaciones uno a uno deben ser opcionales. Para obtener más información sobre cómo se definen las relaciones uno a uno en el Kotlin SDK, consulta Definir una propiedad de relación uno a uno.

Usando los objetos del ejemplo anterior, considere un caso donde un Frog puede tener un estanque favorito. Podemos agregar una propiedad favoritePond a nuestro modelo Frog que sea un enlace opcional a un objeto Pond.

Frog with To-One Relationship to Pond
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int? = null
// To-one relationship (MUST be optional)
var favoritePond: Pond? = null
}

In the App Services schema, we see the new property translates to a field favoritePond:

  • El campo no está en la matriz required porque es una propiedad opcional.

  • Su tipo es un objectId que enlaza a un objeto Pond específico en la colección Pond distinta. Esto se debe a que definimos la llave primaria en nuestro modelo de Pond como un objectId.

El esquema Pond no cambia. Al ser una relación de uno a uno, es unidireccional; Pond no tiene relación con Frog.

Esquema de aplicación Services
{
"title": "Frog",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"age": {
"bsonType": "long"
},
"favoritePond": {
"bsonType": "objectId"
},
"name": {
"bsonType": "string"
}
}
}

Una relación de muchos a muchos asigna una propiedad a cero o más instancias de otro objeto Realm. Para obtener más información sobre cómo se definen las relaciones muchos a muchos en Kotlin SDK, consulta Definir una Propiedad de Relación de Muchos.

Consider another case where a Frog can have many favorite ponds instead of only one. We add a favoritePonds property to our Frog model that is a list of Pond objects. If the frog has no favorite ponds, this is an empty list. As the frog gets favorite ponds, we can create new Pond objects and append them to the frog's favoritePonds list.

Frog with To-Many Relationship to Pond
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int? = null
// To-many relationship (can have many ponds)
var favoritePonds: RealmList<Pond> = realmListOf()
}

In the App Services schema, we see the new property translates to a favoritePonds field that contains all of the Pond objects related to the Frog object:

  • El campo no está en la matriz required porque es una propiedad opcional.

  • The type of this field is an array of type objectId. This is because we defined the primary key on our Pond model as an objectId.

Again, that the Pond schema doesn't change because the Pond has no relationship back to Frog.

Esquema de aplicación Services
{
"title": "Frog",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"age": {
"bsonType": "long"
},
"favoritePonds": {
"bsonType": "array",
"items": {
"bsonType": "objectId"
}
},
"name": {
"bsonType": "string"
}
}
}

Una relación inversa vincula un objeto con otros objetos que lo referencian en una relación definida uno-a-uno o uno-a-muchos denominada vínculo inverso. Para obtener más información sobre cómo se definen las relaciones inversas en Kotlin SDK, consulta Define una relación inversa.

App Services schemas do not support inverse relationships. This is because inverse relationship represent an implicit relationship in Realm that is automatically updated when the backlink is modified. This means that you cannot directly set the value of an inverse relationship, and the relationship does not exist in Atlas. Instead, Realm derives and updates those relationships for you in the client application based on your Realm object model.

Consider a case where the Pond object has an inverse relationship to the Frog object.

Pond with Inverse Relationship to Frog
class Pond : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
// Backlink to the `Frog` that has this `Pond` as its favorite
val frog: RealmResults<Frog> by backlinks(Frog::favoritePonds)
}
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int? = null
// To-many relationship (can have many ponds)
var favoritePonds: RealmList<Pond> = realmListOf()
}

In the App Services schema, we see the Frog has the to-many relationship to the Pond through the favoritePonds property. However, the frog property that represents the inverse relationship to a Frog from our Pond model is not present. This is because the inverse relationship cannot be explicitly defined in Atlas. However, Realm will update it automatically whenever you add or remove an object from the relationship.

Esquema de aplicación Services
// `Pond` schema in App Services DOES NOT contain the
// `frog` inverse relationship property
{
"title": "Pond",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"name": {
"bsonType": "string"
}
}
}
{
"title": "Frog",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"age": {
"bsonType": "long"
},
"favoritePonds": {
"bsonType": "array",
"items": {
"bsonType": "objectId"
}
},
"name": {
"bsonType": "string"
}
}
}

Embedded objects represent nested data inside of a single, specific parent object. You can reference an embedded object type from parent object types in the same way as you would define a relationship. For more information on how embedded objects are defined in Kotlin SDK, refer to Define an Embedded Object.

Sin embargo, a diferencia de los objetos Realm, los objetos incrustados no se almacenan en su propia colección en Atlas. En su lugar, se almacenan como parte del documento del objeto principal y no se puede acceder a ellas fuera del objeto principal.

In the following example, we have a Frog object with a favoritePond property that references a single embedded Pond object and a Forest object with forestPonds relationship property that references a list of many embedded Pond objects:

Frog and Forest with Embedded Pond Relationships
class Frog : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
var age: Int? = null
// Embed a single object (MUST be optional)
var favoritePond: EmbeddedPond? = null
}
class Forest : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = ""
// Embed multiple objects (can have many ponds)
var forestPonds: RealmList<EmbeddedPond> = realmListOf()
}
class EmbeddedPond : EmbeddedRealmObject {
var name: String? = null
}

En el esquema de Servicios de aplicación, se observa que los objetos incrustados se mapean en documentos de cada tipo principal:

Esquema de aplicación Services
{
"title": "Frog",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"age": {
"bsonType": "long"
},
"favoritePond": {
"title": "EmbeddedPond",
"type": "object",
"required": [],
"properties": {
"name": {
"bsonType": "string"
}
}
},
"name": {
"bsonType": "string"
}
}
}
{
"title": "Forest",
"type": "object",
"required": [
"_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"forestPonds": {
"bsonType": "array",
"items": {
"title": "EmbeddedPond",
"type": "object",
"required": [],
"properties": {
"name": {
"bsonType": "string"
}
}
}
},
"name": {
"bsonType": "string"
}
}
}

Las siguientes tablas muestran cómo los tipos de objeto de Realm se asignan a un tipo BSON de esquema de App Services correspondiente. Para obtener una lista completa de los tipos de esquema de App Services compatibles, sus asignaciones y propiedades disponibles, consulte "Asignación de modelos de datos" en la documentación de App Services.

La siguiente tabla enumera los tipos de dato de Kotlin admitidos y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de aplicación Services.

Para obtener más información sobre los tipos de datos de Kotlin compatibles con el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.

Tipo de datos de Kotlin
Realm Object
Esquema de aplicación Services

String

var stringReq: String = ""
"stringReq": {
"bsonType": "string"
}

Byte

var byteReq: Byte = 0
"byteReq": {
"bsonType": "long"
}

Short

var shortReq: Short = 0
"shortReq": {
"bsonType": "long"
}

Int

var intReq: Int = 0
"intReq": {
"bsonType": "long"
}

Long

var longReq: Long = 0L
"longReq": {
"bsonType": "long"
}

Float

var floatReq: Float = 0.0f
"floatReq": {
"bsonType": "float"
}

Double

var doubleReq: Double = 0.0
"doubleReq": {
"bsonType": "double"
}

Boolean

var boolReq: Boolean = false
"boolReq": {
"bsonType": "bool"
}

Char

var charReq: Char = 'a'
"charReq": {
"bsonType": "long"
}

En la siguiente tabla se enumeran los tipos de datos BSON de MongoDB admitidos y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de App Services.

Para obtener más información sobre los tipos de datos BSON de MongoDB compatibles con el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.

Tipo BSON de MongoDB
Realm Object
Esquema de aplicación Services
var objectIdReq: ObjectId = ObjectId()
"objectIdReq": {
"bsonType": "objectId"
}

Decimal128

var decimal128Req: Decimal128 = Decimal128("123.456")
"decimal128Req": {
"bsonType": "decimal"
}

La siguiente tabla enumera los tipos de datos específicos de Realm compatibles y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de aplicación Services.

Para obtener información sobre los tipos de datos específicos de Realm que admite el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.

Realm-Specific Type
Realm Object
Esquema de aplicación Services
var uuidReq: RealmUUID = RealmUUID.random()
"uuidReq": {
"bsonType": "uuid"
}
var realmInstantReq: RealmInstant = RealmInstant.now()
"realmInstantReq": {
"bsonType": "date"
}
var realmAnyOpt: RealmAny? = RealmAny.create("foo")
"realmAnyOpt": {
"bsonType": "mixed"
}
var mutableRealmIntReq: MutableRealmInt = MutableRealmInt.create(0)
"mutableRealmIntReq": {
"bsonType": "long"
}
var listReq: RealmList<CustomObjectType> = realmListOf()
"listReq": {
"bsonType": "array",
"items": {
"bsonType": "uuid"
}
}
var setReq: RealmSet<String> = realmSetOf()
"setReq": {
"bsonType": "array",
"uniqueItems": true,
"items": {
"bsonType": "string"
}
}
var dictionaryReq: RealmDictionary<String> = realmDictionaryOf()
"dictionaryReq": {
"bsonType": "object",
"additionalProperties": {
"bsonType": "string"
}
}
var realmObjectPropertyOpt: CustomObjectType? = null
"realmObjectPropertyOpt": {
"bsonType": "<PRIMARY_KEY_TYPE>"
}
var embeddedProperty: EmbeddedObjectType? = null
"embeddedProperty": {
"title": "EmbeddedObjectType",
"type": "object",
"required": [],
"properties": {
"name": {
"bsonType": "string"
}
}
}

Volver

Cambiar un modelo de objeto

En esta página