Skip to content

Instantly share code, notes, and snippets.

@danared
Created March 27, 2015 18:46
Show Gist options
  • Save danared/3b8be3d2e3cfc962a75a to your computer and use it in GitHub Desktop.
Save danared/3b8be3d2e3cfc962a75a to your computer and use it in GitHub Desktop.
var breakfastSchema = new mongoose.Schema({
steak: {
type: String,
required: true,
enum: ['flank', 'ribeye'],
validate: function(v) {
// Woops! `this` is equal to the global object!
if (this.eggs >= 4) {
return v === 'flank';
}
}
},
eggs: {
type: Number,
required: true,
min: 2
}
});
var Breakfast = mongoose.model('breakfast', breakfastSchema, 'breakfasts');
var updates = { $set: { steak: 'ribeye', eggs: 4 } };
Breakfast.findOneAndUpdate({}, updates, { runValidators: true }, function(err) {
// No err!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment