lib/document.js
function modify (obj, updateQuery) {
  var keys = Object.keys(updateQuery)
    , firstChars = _.map(keys, function (item) { return item[0]; })
    , dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; })
    , newDoc, modifiers
    ;

  if ((keys.indexOf('_id') !== -1) && (updateQuery._id !== obj._id)) { throw "You cannot change a document's _id"; }

  if ((dollarFirstChars.length !== 0) && (dollarFirstChars.length !== firstChars.length)) {
    throw "You cannot mix modifiers and normal fields";
  }

  if (dollarFirstChars.length === 0) {
    // Simply replace the object with the update query contents
    newDoc = deepCopy(updateQuery);
    newDoc._id = obj._id;
  } else {
    // Apply modifiers
    modifiers = _.uniq(keys);
    newDoc = deepCopy(obj);
    modifiers.forEach(function (m) {
      var keys;

      if (!modifierFunctions[m]) { throw "Unknown modifier " + m; }

      try {
        keys = Object.keys(updateQuery[m]);
      } catch (e) {
        throw "Modifier " + m + "'s argument must be an object";
      }

      keys.forEach(function (k) {
        modifierFunctions[m](newDoc, k, updateQuery[m][k]);
      });
    });
  }

  // Check result is valid and return it
  checkObject(newDoc);
  
  if (obj._id !== newDoc._id) { throw "You can't change a document's _id"; }
  return newDoc;
}
function modify (obj, updateQuery) {
  var keys = Object.keys(updateQuery)
    , firstChars = _.map(keys, function (item) { return item[0]; })
    , dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; })
    , newDoc, modifiers
    ;

  if ((keys.indexOf('_id') !== -1) && (updateQuery._id !== obj._id)) { throw "You cannot change a document's _id"; }

  if ((dollarFirstChars.length !== 0) && (dollarFirstChars.length !== firstChars.length)) {
    throw "You cannot mix modifiers and normal fields";
  }

  if (dollarFirstChars.length === 0) {
    // Simply replace the object with the update query contents
    newDoc = deepCopy(updateQuery);
    newDoc._id = obj._id;
  } else {
    // Apply modifiers
    modifiers = _.uniq(keys);
    newDoc = deepCopy(obj);
    modifiers.forEach(function (m) {
      var keys;

      if (!modifierFunctions[m]) { throw "Unknown modifier " + m; }

      try {
        keys = Object.keys(updateQuery[m]);
      } catch (e) {
        throw "Modifier " + m + "'s argument must be an object";
      }

      keys.forEach(function (k) {
        modifierFunctions[m](newDoc, k, updateQuery[m][k]);
      });
    });
  }

  // Check result is valid and return it
  checkObject(newDoc);
  
  if (obj._id !== newDoc._id) { throw "You can't change a document's _id"; }
  return newDoc;
}
function modify ( obj , updateQuery ) { var keys = Object . keys ( updateQuery ) , firstChars = _ . map ( keys , function ( item ) { return item [ 0 ] ; } ) , dollarFirstChars = _ . filter ( firstChars , function ( c ) { return c === '$' ; } ) , newDoc , modifiers ; if ( ( keys . indexOf ( '_id' ) !== - 1 ) && ( updateQuery . _id !== obj . _id ) ) { throw "You cannot change a document's _id" ; } if ( ( dollarFirstChars . length !== 0 ) && ( dollarFirstChars . length !== firstChars . length ) ) { throw "You cannot mix modifiers and normal fields" ; } if ( dollarFirstChars . length === 0 ) { // Simply replace the object with the update query contents newDoc = deepCopy ( updateQuery ) ; newDoc . _id = obj . _id ; } else { // Apply modifiers modifiers = _ . uniq ( keys ) ; newDoc = deepCopy ( obj ) ; modifiers . forEach ( function ( m ) { var keys ; if ( ! modifierFunctions [ m ] ) { throw "Unknown modifier " + m ; } try { keys = Object . keys ( updateQuery [ m ] ) ; } catch ( e ) { throw "Modifier " + m + "'s argument must be an object" ; } keys . forEach ( function ( k ) { modifierFunctions [ m ] ( newDoc , k , updateQuery [ m ] [ k ] ) ; } ) ; } ) ; } // Check result is valid and return it checkObject ( newDoc ) ; if ( obj . _id !== newDoc . _id ) { throw "You can't change a document's _id" ; } return newDoc ; }
Modify a DB object according to an update query
Modify a DB object according to an update query