如何在猫鼬模式中添加相同类型对象的多个属性

时间:2020-04-14 14:11:33

标签: node.js mongodb mongoose mongoose-schema

如何在月光下为电影添加多种(1,2,3 ... x)类型?

我在电影模式中使用了这种流派模式:

const genreSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 5,
    maxlength: 50
  }
});

电影模式:

const Joi = require('joi');
const mongoose = require('mongoose');
const {genreSchema} = require('./genre');


const Movie = mongoose.model('Movie', new mongoose.Schema({
  title:  {
    type: String,
    required: true,
    trim: true,
    minlength: 5,
    maxlength: 255,
  },
  genre: {
    type: genreSchema,
    required: true
  },
  numberInStock: {
    type: Number,
    required: true,
    min: 0,
    max:255
}
}));

function validateMovie(movie) {
  const schema = {
    title: Joi.string().min(3).required(),
    genreId: Joi.objectId().required(),
    numberInStock: Joi.number().min(0).required(),
  };

  return Joi.validate(movie, schema);
}

我还想用Joi验证模式。

1 个答案:

答案 0 :(得分:1)

多种类型?您可以通过以下方式定义模型:

 genre: [{
    type: genreSchema,
    required: true
  }],