Skip to content

Instantly share code, notes, and snippets.

@eamelink
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamelink/93d536e8103e02c636f9 to your computer and use it in GitHub Desktop.
Save eamelink/93d536e8103e02c636f9 to your computer and use it in GitHub Desktop.
MongoDB findAndModify
// Sane implementation of findAndModify:
findAndModify(update: Record => Record, new: Record) {
getOld match {
case None => save(new)
case Some(oldRecord) => save(update(oldRecord))
}
}
// MongodDb implementation of findAndModify:
findAndModify(update: Record => Record, new: Record, always: Record) {
getOld match {
case None =>
val emptyRecord = {}
val emptyUpdated = update(emptyRecord)
save(merge(emptyUpdate, new, always))
case Some(oldRecord) =>
val updated = ifExists(oldRecord)
save(merge(updated, always))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment