Created
April 29, 2013 16:29
-
-
Save davetapley/5482763 to your computer and use it in GitHub Desktop.
Validate a Mongoose model and then upsert if valid
This file contains hidden or 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
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