-
-
Save tdrozdowski/4451005fb1f31c730f75 to your computer and use it in GitHub Desktop.
Set of examples of how to use ReactiveMongo with the Play-ReactiveMongo Plugin.
This file contains 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
# mongo settings | |
mongodb.uri="mongodb://reactive:<password>@paulo.mongohq.com:10065/reactive" |
This file contains 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
def watchCollection = WebSocket.using[JsValue] { request => | |
// Inserts the received messages into the capped collection | |
val in = Iteratee.flatten(futureCollection.map(collection => Iteratee.foreach[JsValue] { json => | |
Logger.debug("received " + json) | |
collection.insert(json) | |
})) | |
// Enumerates the capped collection | |
val out = { | |
val futureEnumerator = futureCollection.map { collection => | |
// so we are sure that the collection exists and is a capped one | |
val cursor: Cursor[JsValue] = collection | |
// we want all the documents | |
.find(Json.obj()) | |
// the cursor must be tailable and await data | |
.options(QueryOpts().tailable.awaitData) | |
.cursor[JsValue] | |
// ok, let's enumerate it | |
cursor.enumerate | |
} | |
Enumerator.flatten(futureEnumerator) | |
} | |
// We're done! | |
(in, out) | |
} |
This file contains 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
def getProfilePic(email : String) = Action { | |
Async { | |
users.find(Json.obj("email" -> email)).one[JsValue].filter(_.isDefined).flatMap { | |
maybeUser => | |
val profileId = (maybeUser.get \ "profilePic" \ "_id" \ "$oid").as[String] | |
serve(gridFS, gridFS.find(BSONDocument("_id" -> new BSONObjectID(profileId)))) | |
} | |
} | |
} |
This file contains 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
400:play.modules.reactivemongo.ReactiveMongoPlugin |
This file contains 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
def update(email : String) = Action(parse.json) { | |
request => | |
Async { | |
users.update(findByEmail(email), request.body).map { | |
lastError => | |
if (lastError.ok) | |
Ok(Json.obj("results" -> "success")) | |
else | |
BadRequest(Json.obj("error" -> s"Error occurred: ${lastError.errMsg.getOrElse("No details. Argh!")}")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment