Skip to content

Instantly share code, notes, and snippets.

@sgodbillon
Created August 25, 2012 14:36
Show Gist options
  • Save sgodbillon/3466575 to your computer and use it in GitHub Desktop.
Save sgodbillon/3466575 to your computer and use it in GitHub Desktop.
// 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