Skip to content

Instantly share code, notes, and snippets.

@tdrozdowski
Last active December 22, 2015 04:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tdrozdowski/056744d6ecf6707b29c3 to your computer and use it in GitHub Desktop.
Reactive Mongo - Core : Example Intended to be used via Scala REPL with SBT. You can setup these files in a basic SBT project and cut-n-paste the example Scala into the REPL and play around with the results to get a feel for the base API. This simple example is taken from reactivemongo.org main page - with corrections added as necessary. Setup y…
scalaVersion := "2.10.1"
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "reactivemongo" % "0.9"
)
import reactivemongo.api._
import reactivemongo.core.actors._
import scala.concurrent.ExecutionContext.Implicits.global
// gets an instance of the driver
// (creates an actor system)
val driver = new MongoDriver
val connection = driver.connection(List("paulo.mongohq.com:10065"), List(Authenticate("reactive", "reactive", "r34ct1v3")))
val db = connection("reactive")
val collection = db("users")
// congrats - now connected to the db in MongoHQ!
<!-- You want this so you do not get spammed by the actor system logging -->
<!-- place in src/main/resources of your SBT project before you start 'sbt console '-->
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
</encoder>
</appender>
<logger name="akka://mongodb-akka" level="INFO" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
db.users.drop();
db.users.insert([
{"email" : "john@bar.com", "firstName" : "John", "lastName" : "Bar", "password" : "unguessable" },
{"email" : "blaz@blee.com", "firstName" : "Blaz", "lastName" : "Blee", "password" : "goaway" },
{"email" : "john@appleseed.com", "firstName" : "John", "lastName" : "Appleseed", "password" : "unguessable" },
{"email" : "foo@bar.com", "firstName" : "Foo", "lastName" : "Bar", "password" : "unguessable" },
{"email" : "jsmith@mongo.com", "firstName" : "John", "lastName" : "Smith", "password" : "unguessable" },
]);
// NOTE - this presumes you have loaded the prior Scala file into your REPL. If not, stop and do that now.
import reactivemongo.bson._
import play.api.libs.iteratee._
// Select only the documents which field 'firstName' equals 'Foo'
val query = BSONDocument("firstName" -> "John")
// select only the fields 'email' and '_id'
val filter = BSONDocument(
"email" -> 1,
"_id" -> 1
)
// Get a cursor of BSONDocuments
val cursor = collection.find(query, filter).cursor[BSONDocument]
/* Let's enumerate this cursor and print a readable
* representation of each document in the response */
cursor.enumerate.apply(Iteratee.foreach { doc =>
println("found document:\n" + BSONDocument.pretty(doc))
})
// Or, the same with getting a list
val cursor2 = collection.find(query, filter).cursor[BSONDocument]
cursor2.toList.map {
list =>
list.foreach { doc =>
println("found document:\n" + BSONDocument.pretty(doc))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment