MongoDB的Scala驱动:ReactiveMongo
jopen
12年前
ReactiveMongo 是一个NOSQL MongoDB 的 Scala 驱动,提供完整的非堵塞和异步 I/O 操作。
运行一个简单的查询:
package foo import reactivemongo.api._ import reactivemongo.bson._ import reactivemongo.bson.handlers.DefaultBSONHandlers._ import play.api.libs.iteratee.Iteratee object Samples { import scala.concurrent.ExecutionContext.Implicits.global def listDocs() = { // select only the documents which field 'firstName' equals 'Jack' val query = BSONDocument("firstName" -> BSONString("Jack")) // get a Cursor[DefaultBSONIterator] val cursor = collection.find(query) // 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: " + DefaultBSONIterator.pretty(doc.bsonIterator)) }) // or, the same with getting a list val cursor2 = collection.find(query) val futurelist = cursor2.toList futurelist.onSuccess { case list => val names = list.map(_.getAs[BSONString]("lastName").get.value) println("got names: " + names) } } }