Skip to content

Instantly share code, notes, and snippets.

@danared
Created March 27, 2015 18:51
Show Gist options
  • Save danared/f5f193f7d6c8129f7b44 to your computer and use it in GitHub Desktop.
Save danared/f5f193f7d6c8129f7b44 to your computer and use it in GitHub Desktop.
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