function walkUpdatePath(schema, obj, op, strict, pref) {
  var prefix = pref ? pref + '.' : '',
      keys = Object.keys(obj),
      i = keys.length,
      hasKeys = false,
      schematype,
      key,
      val;

  var useNestedStrict = schema.options.useNestedStrict;

  while (i--) {
    key = keys[i];
    val = obj[key];

    if (val && val.constructor.name === 'Object') {
      // watch for embedded doc schemas
      schematype = schema._getSchema(prefix + key);
      if (schematype && schematype.caster && op in castOps) {
        // embedded doc schema
        hasKeys = true;

        if ('$each' in val) {
          obj[key] = {
            $each: castUpdateVal(schematype, val.$each, op)
          };

          if (val.$slice != null) {
            obj[key].$slice = val.$slice | 0;
          }

          if (val.$sort) {
            obj[key].$sort = val.$sort;
          }

          if (!!val.$position || val.$position === 0) {
            obj[key].$position = val.$position;
          }
        } else {
          obj[key] = castUpdateVal(schematype, val, op);
        }
      } else if (op === '$currentDate') {
        // $currentDate can take an object
        obj[key] = castUpdateVal(schematype, val, op);
        hasKeys = true;
      } else if (op === '$set' && schematype) {
        obj[key] = castUpdateVal(schematype, val, op);
        hasKeys = true;
      } else {
        var pathToCheck = (prefix + key);
        var v = schema._getPathType(pathToCheck);
        var _strict = strict;
        if (useNestedStrict &&
            v &&
            v.schema &&
            'strict' in v.schema.options) {
          _strict = v.schema.options.strict;
        }

        if (v.pathType === 'undefined') {
          if (_strict === 'throw') {
            throw new StrictModeError(pathToCheck);
          } else if (_strict) {
            delete obj[key];
            continue;
          }
        }

        // gh-2314
        // we should be able to set a schema-less field
        // to an empty object literal
        hasKeys |= walkUpdatePath(schema, val, op, strict, prefix + key) ||
            (utils.isObject(val) && Object.keys(val).length === 0);
      }
    } else {
      var checkPath = (key === '$each' || key === '$or' || key === '$and') ?
        pref : prefix + key;
      schematype = schema._getSchema(checkPath);

      var pathDetails = schema._getPathType(checkPath);
      var isStrict = strict;
      if (useNestedStrict &&
          pathDetails &&
          pathDetails.schema &&
          'strict' in pathDetails.schema.options) {
        isStrict = pathDetails.schema.options.strict;
      }

      var skip = isStrict &&
          !schematype &&
          !/real|nested/.test(pathDetails.pathType);

      if (skip) {
        if (isStrict === 'throw') {
          throw new StrictModeError(prefix + key);
        } else {
          delete obj[key];
        }
      } else {
        // gh-1845 temporary fix: ignore $rename. See gh-3027 for tracking
        // improving this.
        if (op === '$rename') {
          hasKeys = true;
          continue;
        }

        hasKeys = true;
        obj[key] = castUpdateVal(schematype, val, op, key);
      }
    }
  }
  return hasKeys;
}
/*!
 * Walk each path of obj and cast its values
 * according to its schema.
 *
 * @param {Schema} schema
 * @param {Object} obj - part of a query
 * @param {String} op - the atomic operator ($pull, $set, etc)
 * @param {Boolean|String} strict
 * @param {String} pref - path prefix (internal only)
 * @return {Bool} true if this path has keys to update
 * @api private
 */
Walk each path of obj and cast its values
according to its schema.


Walk each path of obj and cast its values according to its schema .
function walkUpdatePath(schema, obj, op, strict, pref) {
  var prefix = pref ? pref + '.' : '',
      keys = Object.keys(obj),
      i = keys.length,
      hasKeys = false,
      schematype,
      key,
      val;
  var useNestedStrict = schema.options.useNestedStrict;
  while (i--) {
    key = keys[i];
    val = obj[key];
    if (val && val.constructor.name === 'Object') {
      schematype = schema._getSchema(prefix + key);
      if (schematype && schematype.caster && op in castOps) {
        hasKeys = true;
        if ('$each' in val) {
          obj[key] = {
            $each: castUpdateVal(schematype, val.$each, op)
          };
          if (val.$slice != null) {
            obj[key].$slice = val.$slice | 0;
          }
          if (val.$sort) {
            obj[key].$sort = val.$sort;
          }
          if (!!val.$position || val.$position === 0) {
            obj[key].$position = val.$position;
          }
        } else {
          obj[key] = castUpdateVal(schematype, val, op);
        }
      } else if (op === '$currentDate') {
        obj[key] = castUpdateVal(schematype, val, op);
        hasKeys = true;
      } else if (op === '$set' && schematype) {
        obj[key] = castUpdateVal(schematype, val, op);
        hasKeys = true;
      } else {
        var pathToCheck = (prefix + key);
        var v = schema._getPathType(pathToCheck);
        var _strict = strict;
        if (useNestedStrict &&
            v &&
            v.schema &&
            'strict' in v.schema.options) {
          _strict = v.schema.options.strict;
        }
        if (v.pathType === 'undefined') {
          if (_strict === 'throw') {
            throw new StrictModeError(pathToCheck);
          } else if (_strict) {
            delete obj[key];
            continue;
          }
        }
        hasKeys |= walkUpdatePath(schema, val, op, strict, prefix + key) ||
            (utils.isObject(val) && Object.keys(val).length === 0);
      }
    } else {
      var checkPath = (key === '$each' || key === '$or' || key === '$and') ?
        pref : prefix + key;
      schematype = schema._getSchema(checkPath);
      var pathDetails = schema._getPathType(checkPath);
      var isStrict = strict;
      if (useNestedStrict &&
          pathDetails &&
          pathDetails.schema &&
          'strict' in pathDetails.schema.options) {
        isStrict = pathDetails.schema.options.strict;
      }
      var skip = isStrict &&
          !schematype &&
          !/real|nested/.test(pathDetails.pathType);
      if (skip) {
        if (isStrict === 'throw') {
          throw new StrictModeError(prefix + key);
        } else {
          delete obj[key];
        }
      } else {
        if (op === '$rename') {
          hasKeys = true;
          continue;
        }
        hasKeys = true;
        obj[key] = castUpdateVal(schematype, val, op, key);
      }
    }
  }
  return hasKeys;
}
function walkUpdatePath ( schema , obj , op , strict , pref ) { var prefix = pref ? pref + '.' : '' , keys = Object . keys ( obj ) , i = keys . length , hasKeys = false , schematype , key , val ; var useNestedStrict = schema . options . useNestedStrict ; while ( i -- ) { key = keys [ i ] ; val = obj [ key ] ; if ( val && val . constructor . name === 'Object' ) { schematype = schema . _getSchema ( prefix + key ) ; if ( schematype && schematype . caster && op in castOps ) { hasKeys = true ; if ( '$each' in val ) { obj [ key ] = { $each : castUpdateVal ( schematype , val . $each , op ) } ; if ( val . $slice != null ) { obj [ key ] . $slice = val . $slice | 0 ; } if ( val . $sort ) { obj [ key ] . $sort = val . $sort ; } if ( ! ! val . $position || val . $position === 0 ) { obj [ key ] . $position = val . $position ; } } else { obj [ key ] = castUpdateVal ( schematype , val , op ) ; } } else if ( op === '$currentDate' ) { obj [ key ] = castUpdateVal ( schematype , val , op ) ; hasKeys = true ; } else if ( op === '$set' && schematype ) { obj [ key ] = castUpdateVal ( schematype , val , op ) ; hasKeys = true ; } else { var pathToCheck = ( prefix + key ) ; var v = schema . _getPathType ( pathToCheck ) ; var _strict = strict ; if ( useNestedStrict && v && v . schema && 'strict' in v . schema . options ) { _strict = v . schema . options . strict ; } if ( v . pathType === 'undefined' ) { if ( _strict === 'throw' ) { throw new StrictModeError ( pathToCheck ) ; } else if ( _strict ) { delete obj [ key ] ; continue ; } } hasKeys |= walkUpdatePath ( schema , val , op , strict , prefix + key ) || ( utils . isObject ( val ) && Object . keys ( val ) . length === 0 ) ; } } else { var checkPath = ( key === '$each' || key === '$or' || key === '$and' ) ? pref : prefix + key ; schematype = schema . _getSchema ( checkPath ) ; var pathDetails = schema . _getPathType ( checkPath ) ; var isStrict = strict ; if ( useNestedStrict && pathDetails && pathDetails . schema && 'strict' in pathDetails . schema . options ) { isStrict = pathDetails . schema . options . strict ; } var skip = isStrict && ! schematype && ! / real|nested / . test ( pathDetails . pathType ) ; if ( skip ) { if ( isStrict === 'throw' ) { throw new StrictModeError ( prefix + key ) ; } else { delete obj [ key ] ; } } else { if ( op === '$rename' ) { hasKeys = true ; continue ; } hasKeys = true ; obj [ key ] = castUpdateVal ( schematype , val , op , key ) ; } } } return hasKeys ; }
Walk each path of obj and cast its values
 according to its schema.
Walk each path of obj and cast its values according to its schema .
// watch for embedded doc schemas // embedded doc schema // $currentDate can take an object // gh-2314 // we should be able to set a schema-less field // to an empty object literal // gh-1845 temporary fix: ignore $rename. See gh-3027 for tracking // improving this.