Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!