@@ -435,8 +435,18 @@ function init(self, obj, doc, prefix) {
435435 */
436436
437437for ( var k in hooks ) {
438- if ( k === 'pre' || k === ' post') {
438+ if ( k === 'post' ) {
439439 Document . prototype [ '$' + k ] = Document [ '$' + k ] = hooks [ k ] ;
440+ } else if ( k === 'pre' ) {
441+ Document . prototype . $pre = Document . $pre = function mongoosePreWrapper ( ) {
442+ if ( arguments [ 0 ] === 'set' ) {
443+ // Make set hooks also work for `$set`
444+ var $setArgs = Array . prototype . slice . call ( arguments ) ;
445+ $setArgs [ 0 ] = '$set' ;
446+ hooks . pre . apply ( this , $setArgs ) ;
447+ }
448+ return hooks . pre . apply ( this , arguments ) ;
449+ } ;
440450 } else {
441451 Document . prototype [ k ] = Document [ k ] = hooks [ k ] ;
442452 }
@@ -468,29 +478,7 @@ Document.prototype.update = function update() {
468478} ;
469479
470480/**
471- * Sets the value of a path, or many paths.
472- *
473- * ####Example:
474- *
475- * // path, value
476- * doc.set(path, value)
477- *
478- * // object
479- * doc.set({
480- * path : value
481- * , path2 : {
482- * path : value
483- * }
484- * })
485- *
486- * // on-the-fly cast to number
487- * doc.set(path, value, Number)
488- *
489- * // on-the-fly cast to string
490- * doc.set(path, value, String)
491- *
492- * // changing strict mode behavior
493- * doc.set(path, value, { strict: false });
481+ * Alias for `set()`, used internally to avoid conflicts
494482 *
495483 * @param {String|Object } path path or object of key/vals to set
496484 * @param {Any } val the value to set
@@ -785,8 +773,36 @@ Document.prototype.$set = function(path, val, type, options) {
785773 return this ;
786774} ;
787775
788- /*!
789- * ignore
776+ /**
777+ * Sets the value of a path, or many paths.
778+ *
779+ * ####Example:
780+ *
781+ * // path, value
782+ * doc.set(path, value)
783+ *
784+ * // object
785+ * doc.set({
786+ * path : value
787+ * , path2 : {
788+ * path : value
789+ * }
790+ * })
791+ *
792+ * // on-the-fly cast to number
793+ * doc.set(path, value, Number)
794+ *
795+ * // on-the-fly cast to string
796+ * doc.set(path, value, String)
797+ *
798+ * // changing strict mode behavior
799+ * doc.set(path, value, { strict: false });
800+ *
801+ * @param {String|Object } path path or object of key/vals to set
802+ * @param {Any } val the value to set
803+ * @param {Schema|String|Number|Buffer|* } [type] optionally specify a type for "on-the-fly" attributes
804+ * @param {Object } [options] optionally specify options that modify the behavior of the set
805+ * @api public
790806 */
791807
792808Document . prototype . set = Document . prototype . $set ;
0 commit comments