MongoDB的Scala驅動:ReactiveMongo
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)
}
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!