限制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'] } } );
- 如何使用NodeJS以超级用户身份连接到另一个MongoDB数据库?
- 集合中的更改后,没有更新的mongoosecaching
- 如何dynamic创buildmongoose模式?
- 我怎么能创build我的mongodb查询基于node.js用户的input?
- Node.js-Ejs-Mongojs堆栈上的“没有方法”forEach“TypeError”
- 如何在Mongoose中添加一个模式的引用到另一个模式并通过json插入数据?
- 传递一个model.find作为被调用的参数会导致错误
- mongoose更新MongoDB中的字段不工作
- 附加更多的属性,从NodeJS(快速)+ MongoDB检索结果 – 模拟SQL连接