Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /
数据类型

混合 - Node.js SDK

在版本12.9.0中进行了更改: 混合属性可以包含混合数据的列表或字典。

10.5.0 版本中的新增功能

混合数据类型是一种域属性类型,可以保存除嵌入式对象或设立之外的任何有效Realm数据类型。 您可以创建 mixed类型的集合(列表、集和字典)。 使用混合数据类型的属性还可以保存 null 值。

注意

混合数据类型是可索引的,但不能将其用作主键。由于 null 是允许值,因此您不能将混合属性声明为可选。

要将对象模型属性的属性设立为混合,请将该属性的类型设置为“ mixed ”。

const DogSchema = {
name: "Dog",
properties: {
name: "string",
birthDate: "mixed",
},
};

通过运行 域 .create()创建具有混合值的对象 写事务(write transaction)中的方法。

realm.write(() => {
// create a Dog with a birthDate value of type string
realm.create("Dog", { name: "Euler", birthDate: "December 25th, 2017" });
// create a Dog with a birthDate value of type date
realm.create("Dog", {
name: "Blaise",
birthDate: new Date("August 17, 2020"),
});
// create a Dog with a birthDate value of type int
realm.create("Dog", {
name: "Euclid",
birthDate: 10152021,
});
// create a Dog with a birthDate value of type null
realm.create("Dog", {
name: "Pythagoras",
birthDate: null,
});
});

在 SDK v 12.9.0及更高版本中,混合数据类型可以保存混合元素的集合(列表或字典,但不是设立)。 您可以使用混合集合对非结构化或可变数据进行建模。 有关更多信息,请参阅定义非结构化数据。

  • 您最多可以嵌套100级混合集合。

  • 您可以查询混合集合属性并注册变更监听器,就像查询普通集合一样。

  • 您可以查找并更新单个混合集合元素

  • 不能在混合集合中存储集合或嵌入式对象。

要使用混合集合,请在数据模型中定义混合类型属性。 然后,创建列表或字典集合。

通过运行Collection.filtered()查询具有混合值的对象 方法,并传入针对非混合字段的过滤。 然后,您可以打印混合属性的值或整个对象本身。

// To query for Blaise's birthDate, filter for his name to retrieve the realm object.
// Use dot notation to access the birthDate property.
let blaiseBirthDate = realm.objects("Dog").filtered(`name = 'Blaise'`)[0]
.birthDate;
console.log(`Blaise's birth date is ${blaiseBirthDate}`);

后退

在此页面上