日期:2014-05-16  浏览次数:20373 次

mongodb(5)Clients -Scala - Scala Casbah

mongodb(5)Clients -Scala - Scala Casbah

4. Clients
Casbah is wrappers and extensions to the Java Driver. It is a layer on top of the official mongo-java-driver.

Using Casbah
Add this to build.sbt file.
"org.mongodb"???????? %?? "casbah_2.10"?????????????? % "2.6.4"?

Here is my Test App
package com.sillycat.easynosqlscala.app

import com.mongodb.casbah.Imports._

object TestConnectionApp extends App{
? //connection
? val rs1 = new ServerAddress("localhost", 27017)
? val rs2 = new ServerAddress("localhost", 27018)
? val rs3 = new ServerAddress("localhost", 27019)

? val client = MongoClient(List(rs1, rs2, rs3))

? //database
? val db = client("test")
? println("previous tables = " + db.collectionNames)

? db.dropDatabase()

? //collection
? val colUsers = db("users")

? //create
? val a = MongoDBObject("name" -> "Carl", "nickName" -> "sillycat", "age" -> 31)
? val b = MongoDBObject("name" -> "Rachel", "nickName" -> "kiko", "age" -> 27)

? colUsers.insert(a)
? colUsers.insert(b)

? //count
? println("number of the users = " + colUsers.count() )

? //query
? val all = colUsers.find()
? all.foreach { item =&g