Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created April 29, 2013 16:29
Show Gist options
  • Save davetapley/5482763 to your computer and use it in GitHub Desktop.
Save davetapley/5482763 to your computer and use it in GitHub Desktop.
Validate a Mongoose model and then upsert if valid
mySchema.statics.validateAndUpsert = function (conditions, update, callback) {
var updateModel = new this(update);
updateModel.validate(function (err) {
if (err) {
callback(err);
} else {
this.findOneAndUpdate(conditions, update, { upsert: true}, callback);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment