Created
March 27, 2015 18:51
-
-
Save danared/f5f193f7d6c8129f7b44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var breakfastSchema = new mongoose.Schema({ | |
steak: { | |
type: String, | |
required: true, | |
enum: ['flank', 'ribeye'], | |
}, | |
eggs: { | |
type: Number, | |
required: true, | |
min: 2 | |
} | |
}); | |
// Pre hook for `findOneAndUpdate` | |
breakfastSchema.pre('findOneAndUpdate', function(next) { | |
this.options.runValidators = true; | |
next(); | |
}); | |
var Breakfast = db.model('breakfast', breakfastSchema, 'breakfasts'); | |
var updates = { $set: { steak: 'ribeye', eggs: 1 } }; | |
// Note the lack of the `runValidators` option | |
Breakfast.findOneAndUpdate({}, updates, function(err) { | |
// "ValidationError: Path `eggs` (1) is less than minimum allowed value (2)." | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment