I'm trying to create:
var mongoose = require('mongoose');
var FeelingSchema = new mongoose.Schema({
userId: String,
feelingDate: Date,
feelingTimeOfDay: String,
feelingValue: String
}
);
How do I restrict the value of the field feelingValue to a limited set, say ['happy', 'angry', 'shocked']
I'm using version 3.8.23 of mongoose
The limit() method in Mongoose is used to specify the number or a maximum number of documents to return from a query.
You can also set the default schema option to a function. Mongoose will execute that function and use the return value as the default.
It's basically there to ensure the strings you save through the schema are properly trimmed.
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ). If you don't want to use this version key you can use the versionKey: false as mongoose.
You can constrain a string field to a set of enumerated values with the enum
attribute in the schema definition:
var FeelingSchema = new mongoose.Schema({
userId: String,
feelingDate: Date,
feelingTimeOfDay: String,
feelingValue: { type: String, enum: ['happy', 'angry', 'shocked'] }
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With