0

I want build an app on NodeJs which will analyze some information that i will input. Currently i can code for one schema. But i cant do nested. Can anybody help me through this. Thanks in advance.

var provinceSchema = new mongoose.Schema({ city: String });

Inside every city i want to put temparture, population.

rizvi770
  • 3
  • 4
  • Does this answer your question? [Nested objects in mongoose schemas](https://stackoverflow.com/questions/39596625/nested-objects-in-mongoose-schemas) – mjarraya Feb 10 '20 at 12:37

3 Answers3

0

You can create a schema for the city and add few fields that are required like as cityname, temparature, populations and so on as below.

const cityDetailsSchema = new Schema({
  cityname: {
    type: String,
    max: 24,
    required: [true, 'Please enter cityname']
  },
  temparature: {
    type: String,
    required: true
  },
  populations: {
    type: Number,
    required: true
  },
  nested: cityDetailsNestedSchema

  // you can create more fields like as above.

});

const cityDetailsNestedSchema = new Schema({
    keyName: { 
        type: String, 
        lowercase: true, 
        trim: true 
     } 
})
Juhil Somaiya
  • 873
  • 7
  • 20
  • thanks . i got the idea. Can you please show me how do i many cities details under one province or state? I am meaning this is the model of cityDetails model you have created. Should i create another model for state? if i have to, how should i connect with cityDetails model? I want more specifically , because i am new on NodeJS – rizvi770 Feb 10 '20 at 13:26
  • Great, you can create the relations between the tables like if, Ahmedabad, Surat, Rajkot resides under Gujarat so that you can have many2one kind of relations between them. You can read more about it and how to implement it here. https://bezkoder.com/mongoose-one-to-many-relationship/ let me know if you're failing at any point. – Juhil Somaiya Feb 11 '20 at 03:44
0
const userSchema = new mongoose.Schema( {
    name: {
        type: String,
        required: true,
        trim: true
    },
    password: {
        type: String,
        required: true,
        minlength: 7,
        trim: true,
        validate(value) {
            if (value.toLowerCase().includes('password')) {
                throw new Error("password should not be password ")
            }
        }


    }, email: {
        type: String,
        unique:true,
        required: true,
        trim: true,
        lowercase: true,
        validate(value) {
            if (!validator.isEmail(value)) {
                throw new Error('invalid E-mail')
            }
        }
    },

    age: {
        type: Number,
        default: 0,
        validate(value) {
            if (value < 0) {
                throw new Error('age must be a positive number');
            }
        }
    },
    avatar:{
        type:Buffer
    },
    tokens:[{
        token:{
                type:String,
                required:true,


        }
    }]
},{
    timestamps:true
})
const User = mongoose.model('User',userSchema );

you can make in this manner

  • Can you please show me how do i many cities details under one province or state? I am meaning this is the model of cityDetails model you have created. Should i create another model for state? if i have to, how should i connect with cityDetails model? I want more specifically , because i am new on NodeJS – – rizvi770 Feb 10 '20 at 14:26
  • okay, so you can also use the 1) virtual Schema for the cities and use the populate method to give a reference to the table with the means of the populate method. 2) you can also go for one to many relationship models for the way you want to create the model . Refer to the wat i did – competitive coder Feb 11 '20 at 05:26
0

you can pass another schema using below method:

var nestedSchema = new Schema({
age: {
        type: Number,
     }
});
var mySchema = new Schema({
  nestedField: nestedSchema

});
Mayank Pandav
  • 425
  • 5
  • 20