Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Mongoose Model for Nested Array JSON

I need to create a Mongoose model for the below nested JSON array. The issue I am facing is TLSM01 is a dynamic Key and I am unable to specify it in the model. If I mention entities alone and pass all the json objects as string it is storing as [object] and not data.

 "entities": [
        {
          "TLSM01": [
            {
              "01": {
                "Name": "Light",
                "Properties": [
                  {
                    "state": [
                      {
                        "type": "boolean",
                        "propertyMode": "actuator"
                      }
                    ],
                    "brightness": [
                      {
                        "type": "integer",
                        "propertyMode": "actuator"
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }

Mongoose Model:

var thingsSchema = ({
    "uuid": String,
    "things": String,
    "manufacturerName": String,
    "manufacturerId": String,
    "osName": String,
    "hardwareVersion": String,
    "firmwareVersion": String,
    "entity": [{String}]
})
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Store data in key-value pair

"entities": [
  {
    keyName:'TLSM01',
    data: [
      {
        "01": {
          "Name": "Light",
          "Properties": [
            {
              "state": [
                {
                  "type": "boolean",
                  "propertyMode": "actuator"
                }
              ],
              "brightness": [
                {
                  "type": "integer",
                  "propertyMode": "actuator"
                }
              ]
            }
          ]
        }
      }
    ]
  }
]

Mongoose Model:

var thingsSchema = ({
    "uuid": String,
    "things": String,
    "manufacturerName": String,
    "manufacturerId": String,
    "osName": String,
    "hardwareVersion": String,
    "firmwareVersion": String,
    "entity": [{_id:false,keyName:{type:String},data:[]}]
})
over 4 years ago · Santiago Trujillo Denunciar

0

1.I think you need something structured like this: 2.See how the light value is an array...within the object - must use key value paring in Mongo and ensure you model Json Object can be mapped to you mongoose database - hence you use you data model to input data - so this should work if you augment it..

const blogSchema = new Schema({
  
name:{
    type:String,
    require: true
  },
heat:{
    type:Number,
    Require:true
  },
moisture:{
    type:Number,
    Require:true
  },
light:{
    green:{type:Number, Require:true},
    red:{type:Number, Require:true},
    blue:{type:Number, Require:true},
    white:{type:Number, Require:true}
  },
body:{
    type:String,
    require: true
  }
},{timeStamps:true});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda