Created
August 25, 2012 14:36
-
-
Save sgodbillon/3466575 to your computer and use it in GitHub Desktop.
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
// finds all documents with lastName = Godbillon and replace lastName with GODBILLON | |
def findAndModify() = { | |
val selector = BSONDocument( | |
"lastName" -> BSONString("Godbillon")) | |
val modifier = BSONDocument( | |
"$set" -> BSONDocument("lastName" -> BSONString("GODBILLON"))) | |
val command = FindAndModify( | |
collection.collectionName, | |
selector, | |
Update(modifier, false)) | |
db.command(command).onComplete { | |
case Left(error) => { | |
throw new RuntimeException("got an error while performing findAndModify", error) | |
} | |
case Right(maybeDocument) => println("findAndModify successfully done with original document = " + | |
// if there is an original document returned, print it in a pretty format | |
maybeDocument.map(doc => { | |
// get a BSONIterator (lazy BSON parser) of this document | |
// stringify it with DefaultBSONIterator.pretty | |
DefaultBSONIterator.pretty(doc.bsonIterator) | |
}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment