我们可以将自定义数组定义为Mongoose模式类型吗?

时间:2017-12-12 05:56:28

标签: node.js mongoose mongoose-schema

Mongoose Schema Type

  

在新文件中定义角色数组,   在定义新架构时将该角色模块用作架构类型

//define this role array 
   exports.module = {
        role:['admin','consumer','super_admin']
    };

//can we use the above role array as a schema type when defining schema
    var UserSchema = new mongoose.Schema({
      name: {
        type:String,
        required:true
      },
      profilePic:String,
      email:{
        type:String,
        unique: true,
        lowercase: true,
        required:true
      },
      role:{
        type: [userRole.module.role],
        required:true
      }
    });

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

要创建新的模式类型,您需要从mongoose.SchemaType继承并将相应的属性添加到mongoose。要创建自定义架构类型,请点击以下链接:

http://mongoosejs.com/docs/customschematypes.html

答案 2 :(得分:0)

来自猫鼬文档:

const ToySchema = new Schema({ name: String });

const ToyBoxSchema = new Schema({
  toys: [ToySchema], // <== used here
  buffers: [Buffer],
  strings: [String],
  numbers: [Number]
  // ... etc
});

来源:https://mongoosejs.com/docs/schematypes.html#arrays