首页 > 解决方案 > 如何构建在猫鼬中工作的嵌套模式?

问题描述

我想在 NodeJs 上构建一个应用程序,它将分析我将输入的一些信息。目前我可以为一个模式编码。但我不能嵌套。任何人都可以帮助我解决这个问题。提前致谢。

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

在每个城市里,我想把温度,人口。

标签: node.jsmongoose-schema

解决方案


您可以为城市创建一个模式并添加一些必需的字段,例如城市名称、温度、人口等,如下所示。

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 
     } 
})

推荐阅读