Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
Overview
嵌入式对象是一种特殊类型的Realm 对象,用于对有关特定对象的复杂数据进行建模。 嵌入式对象与关系类似,但它们提供了额外的约束,并且更自然地映射到非规范化的 MongoDB document model 。
Realm 强制执行唯一的所有权约束,将每个嵌入式对象视为单个特定父对象内的嵌套数据。嵌入式对象继承其父对象的生命周期,不能作为独立的 Realm 对象存在。这意味着嵌入式对象不能有主键,并且如果删除嵌入式对象的父对象,Realm 会自动删除嵌入式对象。
提示
嵌入式对象类型可重用、可组合
您可以在多个父对象类型中使用相同的嵌入式对象类型,也可以在其他嵌入式对象中嵌入对象。您甚至可以在其自身的定义中将嵌入式对象类型以递归方式引用为一个可选属性。
Realm 对象模型
要定义嵌入式对象,请将 embedded 设置为 true。您可以采用与定义关系相同的方式,从父对象类型中引用嵌入式对象类型:
重要
嵌入式对象不能有主键。
const AddressSchema = { name: "Address", embedded: true, // default: false properties: { street: "string?", city: "string?", country: "string?", postalCode: "string?", }, }; const ContactSchema = { name: "Contact", primaryKey: "_id", properties: { _id: "objectId", name: "string", address: "Address", // Embed a single object }, }; const BusinessSchema = { name: "Business", primaryKey: "_id", properties: { _id: "objectId", name: "string", addresses: { type: "list", objectType: "Address" }, // Embed an array of objects }, };
JSON schema
嵌入式对象映射到父类型模式中的嵌入式文档。此行为与常规 Realm 对象不同,后者映射到自己的 MongoDB 集合。
{ "title": "Contact", "bsonType": "object", "required": ["_id"], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" }, "address": { "title": "Address", "bsonType": "object", "properties": { "street": { "bsonType": "string" }, "city": { "bsonType": "string" }, "country": { "bsonType": "string" }, "postalCode": { "bsonType": "string" } } } } }
{ "title": "Business", "bsonType": "object", "required": ["_id", "name"], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" }, "addresses": { "bsonType": "array", "items": { "title": "Address", "bsonType": "object", "properties": { "street": { "bsonType": "string" }, "city": { "bsonType": "string" }, "country": { "bsonType": "string" }, "postalCode": { "bsonType": "string" } } } } } }
读取和写入嵌入式对象
创建嵌入式对象
要创建嵌入式对象,请将嵌入式对象的实例分配给父对象的属性:
// create an embedded address object const sydneyOrthodontics = { street: "42 Wallaby Way", city: "Sydney", country: "Australia", postalCode: "2774", }; realm.write(() => { // create a contact object realm.create("Contact", { _id: new BSON.ObjectId(), name: "Philip Sherman", address: sydneyOrthodontics, // embed the address in the contact object }); });
更新嵌入式对象属性
若要更新嵌入式对象中的属性,请在写入事务中修改该属性:
// Find the contact with the address you want to update const harryPotter = realm .objects("Contact") .filtered("name = 'Harry Potter'")[0]; // modify the property of the embedded object in a write transaction realm.write(() => { // update the embedded object directly through the contact harryPotter.address.street = "1 Hogwarts Ave"; });
覆盖嵌入式对象
要覆盖嵌入式对象,请在写事务中将一方的嵌入式对象属性重新分配给新实例。
// create a new address const harryNewAddress = { street: "12 Grimmauld Place", city: "London", country: "UK", postalCode: "E1 7AA", }; realm.write(() => { // overwrite the embedded object with the new address within a write transaction harryPotter.address = harryNewAddress; });
查询嵌入式对象属性的集合
根据嵌入式对象属性值,使用点符号对对象集合进行过滤或排序:
注意
无法直接查询嵌入式对象。而是通过对父对象类型的查询来访问嵌入式对象。
const philipShermanAddress = realm .objects("Contact") .filtered("name = 'Philip Sherman'")[0].address.street; console.log(`Philip Sherman's address is ${philipShermanAddress}`);
删除嵌入式对象
Realm 对嵌入式对象使用级联删除要删除嵌入式对象,请删除嵌入式对象的父对象。
realm.write(() => { // Deleting the contact will delete the embedded address of that contact realm.delete( realm.objects("Contact").filtered("name = 'Philip Sherman'") ); });