Skip to content

Instantly share code, notes, and snippets.

@anarcher
Created September 5, 2010 15:36
Show Gist options
  • Save anarcher/566093 to your computer and use it in GitHub Desktop.
Save anarcher/566093 to your computer and use it in GitHub Desktop.
MongoDB-SoySauce-sketch
val db = MongoDB("hotflow")
db('post) < ("postId" -> 1212 ) :: ("post" -> "text" ) :: ("username" -> "anarcher") :: Nil
db 'post ? ("postId" -> 121) | (0,10)
db('post).find( ("postId" -> 121) ).limit(10).offset(0)
db.getMongoDb
db.getCollection("post")
import com.mongodb._
object MongoDB {
def apply(dbName: String,colName: String,host:String="localhost",port:Int= 27017) = {
val m = new MongoDB(host,port)
val db = m.getDB(dbName)
val col = db.getCollection(colName)
col
}
}
object Post extends MongoDBSchema {
val postId = MongoDBLong
val post = MongoDBString
def apply( ...
}
Post(("postId" -> 1) ) validate
type Schema = {
val fields : List[String]
val required : List[String]
}
trait PostId {
val fields = "postId" :: Nil
val required = "postId" :: Nil
}
trait UserName {
val fields = "userName" :: Nil
val required = "userName"
}
trait PostIdAndUsername extends PostId with UserName
------------------------------------------------------------------------------------------------------
trait PostId extends Schema {
var postId : MongoDBInt = MongoDBInt
}
trait Post extends Schema {
var post : MongoDBString = _
}
col.findOne[PostId,Post](q('postId -> 1 ))
// MongoDBObject extends PostId with Post
@anarcher
Copy link
Author

anarcher commented Sep 5, 2010

Collection기반 인터페이스는 어떨까?

val db = MongoDB("HotFlow")

db('post) << Map( ("postId" -> 1212 ) , ( "post" -> "test") )

db('post) .find(Map("postId" -> 1)

@anarcher
Copy link
Author

anarcher commented Sep 5, 2010

schema 를 따로 추가해서 validator을 명시적으로 하게 하면 되지 않을까? (collection기반 표현과 함께)

@anarcher
Copy link
Author

anarcher commented Sep 5, 2010

스케치입니다. 그냥 이런 저런 인터페이스를 한번 대충 적어 봤어욤. :)

@anarcher
Copy link
Author

anarcher commented Sep 6, 2010

Implicit conversions을 써서 DBCollection에 문법사탕을 좀 추가해도 괜찮을 듯 하기도 하군요. :)

 val col = db("post")
 col << ("postId" -> 1) :: Nil
 col ? ("postId" -> 1) 

@anarcher
Copy link
Author

anarcher commented Sep 6, 2010

문제는 Schema (혹은 Validator) 인데. 뭔가 동적이지만 schema 검증이 필요할때는 하고 싶다는 생각이.

@anarcher
Copy link
Author

anarcher commented Sep 6, 2010

Schema를 일종의 필터로 사용할수 있겠군요.

  object Post  extends MongoDBSchema {
     val postId = MondoDBLong
  }
  db('post) | Post    // Post Schema에 맞는 녀석만 ( 즉  postId만 있는 녀석) 
  db('post) || Post   //  postId만 검증해서 충족되는 모든 녀석 

@anarcher
Copy link
Author

anarcher commented Sep 6, 2010

아무래도 나 림보에 빠진듯. '' 누가 좀 킥 좀. ''

@anarcher
Copy link
Author

anarcher commented Sep 6, 2010

http://gist.github.com/rockdoli 감사합니다. '_' ㅎㅎ :)

@outsideris
Copy link

계정을 날리니까 코멘트도 지워지는군요... 죄송.. ㅎ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment