Mixed - Node.js SDK
On this page
New in version 10.5.0.
Overview
The mixed data type is a realm property type that can hold any valid Realm data type except a collection.
You can create collections (lists, sets, and dictionaries) of type mixed
, but a mixed
itself
cannot be a collection. Properties using the mixed data type can also hold null values.
Note
The mixed data type is indexable, but you can't use it as a primary key. Because null is a permitted value, you can't declare a Mixed property as optional.
Realm Object Models
To set a property of your object model as Mixed
, set the property's type to
"mixed
".
const DogSchema = { name: "Dog", properties: { name: "string", birthDate: "mixed", }, };
Create an Object With a Mixed Value
Create an object with a mixed value by running the realm.create() method within a 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, }); });
Query for Objects with a Mixed Value
Query for objects with a mixed value by running the Collection.filtered() method and passing in a filter for a non-mixed field. You can then print the value of the mixed property or the entire object itself.
// 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}`);