Skip to content

Instantly share code, notes, and snippets.

@ReSTARTR
Created March 6, 2011 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReSTARTR/857260 to your computer and use it in GitHub Desktop.
Save ReSTARTR/857260 to your computer and use it in GitHub Desktop.
sbt project setting.
import com.mongodb.casbah.Imports._
object CasbahSample {
val conn = MongoConnection()
val db = conn("casbah_test")
val collection = db("sample")
// 下記のように1行にまとめてもでもOK
// val collection = MongoConnection()("casbah_test")("sample")
def createRecordAndSave {
// with MongoDBObject
val user = MongoDBObject("id"->1, "name"->"me")
println(user) // { "id" : 1 , "name" : "me"}
// with MongoDBObjectBuilder
val builder = MongoDBObject.newBuilder
builder += "id"->2
builder += "name"->"you"
val user2 = builder.result
// save db objects
collection += user // { "_id" : ObjectId("4d7385ebf21423dcecb4c578"), "id" : 1, "name" : "me" }
collection += user2 // { "_id" : ObjectId("4d7385ebf21423dcedb4c578"), "id" : 2, "name" : "you" }
// find all
collection.find().foreach { println(_) }
}
def convertObjectType {
val user = MongoDBObject("id"->1, "name"->"me")
val user_id = user.getAs[Int]("id")
println(user_id) // Some(1)
// 実値と異なる型で取得しようとした場合はExceptionが発生する
// val user_id2:String = user("id")
// この場合、例外発生しない…なぜ?
val user_id2:Option[String] = user.getAs[String]("id")
println(user_id2) // Some(2)
collection.find().foreach { rowObj =>
val user_id:Option[String] = rowObj.getAs[String]("id") orElse { throw new Exception("type mismatch") }
println(user_id)
}
}
def joinsObjects {
val identity = MongoDBObject("name"->"me", "age"->27)
val address = MongoDBObject("country"->"Japan", "prefecture"->"Tokyo")
val user = identity ++ address
println(user) // { "age" : 27 , "country" : "Japan" , "prefecture" : "Tokyo" , "name" : "me"}
println(user.asDBObject)
}
def createDBList {
val users1 = MongoDBList(
MongoDBObject("name"->"me"),
MongoDBObject("name"->"you"))
println(users1) // [ { "name" : "me"} , { "name" : "you"}]
val users = MongoDBList.newBuilder
val user1 = MongoDBObject("name"->"me")
val user2 = MongoDBObject("name"->"you")
users += user1
users += user2
println(users.result) // [ { "name" : "me"} , { "name" : "you"}]
}
def buildQuery {
// 取得条件を指定
val condition = MongoDBObject("name"->"me")
collection.find(condition).foreach( println )
// 取得フィールドを限定
val fields = MongoDBObject("id"->1)
collection.find(condition, fields).foreach( println )
// 条件の指定をせずに取得フィールドのみ限定する場合
val conditionEmpty = MongoDBObject.empty
collection.find(conditionEmpty, fields).foreach( println )
// DSLを使ってクエリを組み立てる
// ここにあるものは使えるらしい。
// http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperators
collection.find( "name" $exists true ).foreach{ println }
// 組み合わせる場合は"++"でつなげる
collection.find( ("name" $exists true) ++ ("age" $gte 20 $lt 30 ) ).foreach{ println }
val obj = collection.findOne().get
println(obj("name")) // me
}
def main(args: Array[String]) {
createRecordAndSave
convertObjectType
joinsObjects
createDBList
buildQuery
}
}
import sbt._
class SalatTestProject(info: ProjectInfo) extends DefaultProject(info) {
val casbah = "com.mongodb.casbah" % "casbah_2.8.1" % "2.0.2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment