Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongoose 具有不同模式的子文档_Mongoose_Mongoose Schema - Fatal编程技术网

Mongoose 具有不同模式的子文档

Mongoose 具有不同模式的子文档,mongoose,mongoose-schema,Mongoose,Mongoose Schema,我是Mongoose的新手,我正在努力理解如何正确地声明结构 比如说,我有一个单独的集合todo,其中应该包含描述应该做什么的文档。所有TODO项都有一些共同的属性,但大多数细节取决于特定的任务 // Tasks: var hairCutSchema = new Schema({ style: {type: String, required:true}, length: Number }); var paintWallSchema = new Schema({ color: {t

我是Mongoose的新手,我正在努力理解如何正确地声明结构

比如说,我有一个单独的集合
todo
,其中应该包含描述应该做什么的文档。所有TODO项都有一些共同的属性,但大多数细节取决于特定的任务

// Tasks:

var hairCutSchema = new Schema({
  style: {type: String, required:true},
  length: Number
});

var paintWallSchema = new Schema({
  color: {type: String, required:true},
  surface: Number, required:true},
  layers: Number
});

let napSchema = new Schema({
  duration: {type: Number, required:true},
  dream: String,
  pillows: Number,
  // ....
});


// TODOs (parent document):

var todoSchema = new Schema({
  due: Date,
  created: Date,
  task: <either hairCutSchema, paintWallSchema OR napSchema>
});
但是,这种模式不会阻止我同时声明
hairCutTask
napTask
——而且可能所需的子文档字段会使这三种类型中的每一种都成为必需的



构造此类数据的好方法是什么?模式应该是什么样的?

我可能正在寻找“鉴别器”:

我可能正在寻找“鉴别器”:“好吧,这不能用[子文档]来解决,因为我只能为每个字段分配一个子文档类型。”--为此,您可以查看Schema.Types.Mixed(没有创建对集合项的实际引用那么有用),这样的“混合”架构类型不会阻止我存储毫无意义的内容,不会匹配任何任务架构,对吗?“好吧,这不能用[subdocuments]来解决,因为我只能为每个字段分配一个子文档类型。”--为此,您可以查看Schema.Types.Mixed(没有创建对集合项的实际引用那么有用),这样的“混合”架构类型不会阻止我存储无意义的内容,不会匹配任何任务架构,对吗?
var todoSchema = new Schema({
  due: Date,
  created: Date,

  hairCutTask: haitCutSchema,
  paintWallTask: paintWallSchema,
  napTask: napSchema
});