Mongodb 索引大小不同的JSON对象的架构
基本上,我有一个平均堆栈应用程序,我想在其中定义一个数组大小变化的模式。有人告诉我这是可能的 我已经成功地在MongoDB上保存了一个JSON对象,如下所示Mongodb 索引大小不同的JSON对象的架构,mongodb,mongoose,backend,mean-stack,mongoose-schema,Mongodb,Mongoose,Backend,Mean Stack,Mongoose Schema,基本上,我有一个平均堆栈应用程序,我想在其中定义一个数组大小变化的模式。有人告诉我这是可能的 我已经成功地在MongoDB上保存了一个JSON对象,如下所示 { "Diseases":[ { "DiseaseName": "Diabetes", "Severity": "3"
{
"Diseases":[
{
"DiseaseName": "Diabetes",
"Severity": "3"
},
{
"DiseaseName": "Psoriasis",
"Severity": "4"
}
],
"Prescriptions": [
{
"Name" : "Insulin",
"Unit" : "100 microgram"
},
{
"Name" : "Cortisone",
"Unit" : "150 microgram"
}
]
}
基本上,我想定义一个模式,其中疾病和处方中的数组元素(对象)的数量可以是任意的。如果我理解正确的话,疾病和处方是一个数组,它包含一个对象中包含的对象。从您的问题来看,您似乎需要数据库中对象数组的模式。这是你可以做到的
mongoose.Schema({
Diseases: [
{
DiseaseName: String,
Severity: Number,
},
],
Prescriptions: [
{
Name: String,
Unit: String,
},
],
});
以下是您可以使用的示例架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const diseaseSchema = new Schema({
diseaseName:{
type: String,
required: true,
},
severity: {
type: Number,
min:0,
max: 5,
required: true
}
},{
timestamps: true
});
const prescriptionSchema = new Schema({
name:{
type: String,
required: true,
},
unit: {
type: String,
required: true
}
},{
timestamps: true
});
const patientSchema = new Schema({
diseases: [diseaseSchema]
prescriptions: [prescriptionSchema]
},{
timestamps: true
});
var Patients = mongoose.model('Patient', patientSchema);
module.exports = Patients;
参考调查无法了解您的问题您能澄清一下吗?你说的武断是什么意思?它应该是一个常数吗?或无限。如果您不想限制数组的大小,它是由mongoose设置的,因此您不必担心。在提出问题时请更具体一点,并多注意一点:始终先阅读堆栈的文档,任意意味着没有设置的编号,它可以通过任何方式进行设置。例如,一个病人可能有任何数量的疾病(或者根本没有疾病!),与处方相似。我不知道如何在后端为此编写模式(通常在patient.model.js中)