secret
Last active

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 your project directory as such: *$projectRoot: build.sbt *$projectRoot: /src/main/resources/logback.xml Then run: 'sbt console' from the root directory, and copy/paste the .scala gist contents to experiment with things a bit.

  • Download Gist
build.sbt
Scala
1 2 3 4 5 6 7
scalaVersion := "2.10.1"
 
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
 
libraryDependencies ++= Seq(
"org.reactivemongo" %% "reactivemongo" % "0.9"
)
connect.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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!
logback.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- 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>
mongo-seed.js
JavaScript
1 2 3 4 5 6 7 8 9 10
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" },
]);
query.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 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))
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.