0

let say, i have some data that contain state (country), province, and year, and the conditions, i will try to explain with this below:

state_a: {
    province: {
        name: 'prov_1',
        conditions: {
           year: 2019,
           condition: 'normal'
        },
        conditions: {
           year: 2020,
           condition: 'not normal'
        }
    },
    province: {
        name: 'prov_b',
        conditions: {
           year: 2019,
           condition: 'not normal'
        },
        conditions: {
           year: 2020,
           condition: 'not normal'
        }
    }
},
state_b: ......... etc

actually, i don't know how to explain it :D, i hope that code could explain what i mean. i want to create model in mongoose. could you help me, guys?

riki yudha
  • 35
  • 1
  • 7

2 Answers2

0

You need to do something like this.

const dataSchema =  new mongoose.Schema({
State: {
        type: String,
        required: true
       },
Province: [{
        name: {
            type: String;
            required: true
           }
        conditions: [
        { 
           year: { 
              type: Number,
              required: true
              },

           condition: {
              type: String,
              required: true
              }
         }]

    }]

})
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

firstly install mongoose npm i mongoose

Then, insert the following code

const mongoose = require('mongoose');
const Schema = mongoose.Schema

const stateSchema = new Schema({
  state: {
    province: [
      {
      name: String,
      conditions: {
        year: Date,
        condition: {type: String, enum: ['normal', 'not normal']},
        }
      },
      {
        name: String,
        conditions: {
          year: Date,
          condition: {type: String, enum: ['normal', 'not normal']},
          }
        },
    ]
  }
})

const state = mongoose.model('state', stateSchema)