Skip to content

Instantly share code, notes, and snippets.

@knubie
Last active August 29, 2015 13:56
Show Gist options
  • Save knubie/8907481 to your computer and use it in GitHub Desktop.
Save knubie/8907481 to your computer and use it in GitHub Desktop.
@update = (collection, query, field, update) ->
# Set up this object to pass in to the mongo update method.
$set = {}
# Create the $set object for mongo's update method by passing
# the doc's 'field' to the 'update' argument.
$set[field] = _.compose(update, _.reduce) field.split('.')
, (memo, accessor) -> memo[accessor] # Get field from the doc.
, collection.findOne(query)
# Update the mongo collection.
collection.update query, {$set}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment