Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda