Last active
August 29, 2015 14:11
-
-
Save eamelink/93d536e8103e02c636f9 to your computer and use it in GitHub Desktop.
MongoDB findAndModify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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