Skip to content

Instantly share code, notes, and snippets.

@duketon
Created June 19, 2015 13:56
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 duketon/bf4d484852722883228e to your computer and use it in GitHub Desktop.
Save duketon/bf4d484852722883228e to your computer and use it in GitHub Desktop.
val client = MongoClients.create(new ConnectionString("localhost:27017"))
val db = client.getDatabase("myDb")
val collection = db.getCollection("myCollection")
def findAndModify(filter: Document, update: Document, options: FindOneAndUpdateOptions): Future[Document] = {
val p: Promise[Document] = Promise()
collection.findOneAndUpdate(
filter, update, options,
new SingleResultCallback[Document] {
override def onResult(result: Document, t: Throwable): Unit =
if (t == null) p.success(result) else p.failure(t)
})
p.future
}
val queryOptions =
new FindOneAndUpdateOptions()
.upsert(true)
.returnDocument(ReturnDocument.AFTER)
.upsert(false)
findAndModify(new Document("lastName", "Smith"), new Document("lastName", "Jones"), queryOptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment