限制猫鼬字段值

时间:2015-04-25 01:18:35

标签: node.js mongodb mongoose

我正在尝试创建:

var mongoose = require('mongoose');

var FeelingSchema = new mongoose.Schema({
    userId: String,
    feelingDate: Date,
    feelingTimeOfDay: String,
    feelingValue: String
  }
);

如何将字段feelingValue的值限制为有限集合,例如['happy','angry','shocked']

我使用的是mongoose版本3.8.23

1 个答案:

答案 0 :(得分:26)

您可以使用架构定义中的enum属性将字符串字段约束为一组枚举值:

var FeelingSchema = new mongoose.Schema({
    userId: String,
    feelingDate: Date,
    feelingTimeOfDay: String,
    feelingValue: { type: String, enum: ['happy', 'angry', 'shocked'] }
  }
);