我正在尝试创建:
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
答案 0 :(得分:26)
您可以使用架构定义中的enum
属性将字符串字段约束为一组枚举值:
var FeelingSchema = new mongoose.Schema({
userId: String,
feelingDate: Date,
feelingTimeOfDay: String,
feelingValue: { type: String, enum: ['happy', 'angry', 'shocked'] }
}
);