限制mongoose字段值

我正在尝试创build:

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

您可以使用模式定义中的enum属性将string字段约束为一组枚举值:

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