node.js - 如何在 mongoose 或 mongodb 中制作数据模型
问题描述
比方说,我有一些包含州(国家)、省和年份的数据,以及条件,我将在下面尝试解释:
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
实际上,我不知道如何解释它:D,我希望代码可以解释我的意思。我想在猫鼬中创建模型。你们能帮帮我吗,伙计们?
解决方案
你需要做这样的事情。
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
}
}]
}]
})
推荐阅读
- java - 在 Android 本机应用程序上保存持久性 cookie
- python-3.x - 有序列时出错 - TypeError:序列项 0:预期的 str 实例,找到列表
- python - 如何对最终输出的数据进行排序?
- arrays - 斯威夫特:用给定的例子拆分 [String] 导致 [[String]] 的正确方法是什么?
- visual-studio - 在 PostBuildEvent - 在 Azure DevOps VSTS 中构建时,$(TargetDir) 与本地计算机不同
- javascript - 是否可以在 dart 中使用 destructure 和 spread 在一个列表中,比如 dart 中的 javascript?
- sql - 如何获取重复计数SQL
- angular - 量角器线束环境和垫子按钮
- android - AsyncListDiffer 没有更新 recyclerview
- mysql - MySql 查询从表中找出前 50% 的记录