Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
Tipos de datos

Mixto - Node.js SDK

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

Novedad en la versión 10.5.0.

El tipo de dato mixto es un tipo de propiedad de dominio que puede contener cualquier tipo de dato válido de dominio, excepto un objeto incrustado o un conjunto. Se pueden crear colecciones (listas, conjuntos y diccionarios) de tipo mixedLas propiedades que utilizan el tipo de datos mixto también pueden contener valores nulos.

Nota

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.

Para establecer una propiedad de tu modelo de objetos como mixta, configura el tipo de propiedad en "mixed".

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

Cree un objeto con un valor mixto ejecutando el Métodorealm.create() dentro de una transacción de escritura.

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,
});
});

In 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.

  • Se pueden anidar colecciones mixtas hasta 100 niveles.

  • You can query mixed collection properties and register a listener for changes, as you would a normal collection.

  • You can find and update individual mixed collection elements

  • You cannot store sets or embedded objects in mixed collections.

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

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}`);

Volver

Sets

En esta página