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/react-native/model-data/data-types/mixed/ below:

Mixed - React Native SDK - Atlas Device SDKs

Changed in version realm@12.9.0: Mixed properties can contain lists or dictionaries of mixed data.

New in version realm@10.5.0.

The Mixed data type is a realm property type that can hold any valid Realm data type except an embedded object or a set. You can create collections (lists, sets, and dictionaries) of type mixed. Properties using the mixed data type can also hold null values.

The Mixed type is indexable, but you can't use it as a primary key.

Properties using the Mixed type can hold null values and cannot be defined as optional. All instances of the JavaScript Number type in a Realm Mixed type are mapped to the Realm double type.

To set a property of your object model as Mixed, set the property's type to mixed.

class Cat extends Realm.Object {  static schema = {    name: 'Cat',    properties: {      name: 'string',      birthDate: 'mixed',    },  };}
class Cat extends Realm.Object<Cat> {  name!: string;  birthDate?: Realm.Mixed;  static schema: ObjectSchema = {    name: 'Cat',    properties: {      name: 'string',      birthDate: 'mixed',    },  };}

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

To use mixed collections, define the mixed type property in your data model. Then, create the list or dictionary collection.

Create an object with a Mixed value by using the new operator within a write transaction.

In the following CreateCatsInput example, we create several Cat realm objects with a Mixed type for the birthDate field.

The CreateCatsInput component does the following:

1const CreateCatsInput = () => {2  const realm = useRealm();34  useEffect(() => {5    6    realm.write(() => {7      8      realm.create('Cat', {9        name: 'Euler',10        birthDate: 'December 25th, 2017',11      });1213      14      realm.create('Cat', {15        name: 'Blaise',16        birthDate: new Date('August 17, 2020'),17      });1819      20      realm.create('Cat', {name: 'Euclid', birthDate: 10152021});2122      23      realm.create('Cat', {name: 'Pythagoras', birthDate: null});24    });25  }, []);2627  28  const cats = useQuery(Cat);2930  return (31    <>32 {cats.map(cat => (33 <View>34 <Text>{cat.name}</Text>35 <Text>{String(cat.birthDate)}</Text>36 </View>37 ))}38 </>39  );40};
1const CreateCatsInput = () => {2  const realm = useRealm();34  useEffect(() => {5    6    realm.write(() => {7      8      realm.create('Cat', {9        name: 'Euler',10        birthDate: 'December 25th, 2017',11      });1213      14      realm.create('Cat', {15        name: 'Blaise',16        birthDate: new Date('August 17, 2020'),17      });1819      20      realm.create('Cat', {name: 'Euclid', birthDate: 10152021});2122      23      realm.create('Cat', {name: 'Pythagoras', birthDate: null});24    });25  }, []);2627  28  const cats = useQuery(Cat);2930  return (31    <>32 {cats.map(cat => (33 <View>34 <Text>{cat.name}</Text>35 <Text>{String(cat.birthDate)}</Text>36 </View>37 ))}38 </>39  );40};

To query for objects with a Mixed value, run the Collection.filtered() method and pass in a filter for a non-Mixed field. You can then print the value of the Mixed property or the entire object itself.

In the following CatInfoCard example, we query for a Cat object using the cat's name.

The CatInfoCard component does the following:

1const CatInfoCard = ({catName}) => {2  3  4  const cat = useQuery(5    Cat,6    cats => {7      return cats.filtered(`name = '${catName}'`);8    },9    [catName],10  )[0];11  const catBirthDate = cat.birthDate;1213  if (cat) {14    return (15      <>16 <Text>{catName}</Text>17 <Text>{String(catBirthDate)}</Text>18 </>19    );20  } else {21    return <Text>Cat not found</Text>;22  }23};
1type CatInfoCardProps = {catName: string};23const CatInfoCard = ({catName}: CatInfoCardProps) => {4  5  6  const cat = useQuery(7    Cat,8    cats => {9      return cats.filtered(`name = '${catName}'`);10    },11    [catName],12  )[0];13  const catBirthDate = cat.birthDate;1415  if (cat) {16    return (17      <>18 <Text>{catName}</Text>19 <Text>{String(catBirthDate)}</Text>20 </>21    );22  } else {23    return <Text>Cat not found</Text>;24  }25};

Because Mixed properties can be more than one type, you can't rely on the property's value being a specific type.

With Object.getPropertyType(), you can get a Mixed property's underlying type. This allows you build your own type checking.

const isString = (  val: Mixed,  name: string,  object: Realm.Object,): val is Realm.Types.String => {  return object.getPropertyType(name) === 'string';};type CatInfoCardProps = {catName: string};const CatInfoCard = ({catName}: CatInfoCardProps) => {  const cat = useQuery(    Cat,    cats => {      return cats.filtered(`name = '${catName}'`);    },    [catName],  )[0];    const catBirthDate = isString(cat.birthDate, 'birthDate', cat)    ? cat.birthDate    : cat.birthDate.toString();  if (cat) {    return (      <> <Text>{catName}</Text> <Text>{catBirthDate}</Text> </>    );  } else {    return <Text>Cat not found</Text>;  }};

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