Created
September 5, 2010 15:36
-
-
Save anarcher/566093 to your computer and use it in GitHub Desktop.
MongoDB-SoySauce-sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Post extends MongoDBSchema { | |
val postId = MongoDBLong | |
val post = MongoDBString | |
def apply( ... | |
} | |
Post(("postId" -> 1) ) validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
schema 를 따로 추가해서 validator을 명시적으로 하게 하면 되지 않을까? (collection기반 표현과 함께)
스케치입니다. 그냥 이런 저런 인터페이스를 한번 대충 적어 봤어욤. :)
Implicit conversions을 써서 DBCollection에 문법사탕을 좀 추가해도 괜찮을 듯 하기도 하군요. :)
val col = db("post") col << ("postId" -> 1) :: Nil col ? ("postId" -> 1)
문제는 Schema (혹은 Validator) 인데. 뭔가 동적이지만 schema 검증이 필요할때는 하고 싶다는 생각이.
Schema를 일종의 필터로 사용할수 있겠군요.
object Post extends MongoDBSchema { val postId = MondoDBLong } db('post) | Post // Post Schema에 맞는 녀석만 ( 즉 postId만 있는 녀석) db('post) || Post // postId만 검증해서 충족되는 모든 녀석
아무래도 나 림보에 빠진듯. '' 누가 좀 킥 좀. ''
http://gist.github.com/rockdoli 감사합니다. '_' ㅎㅎ :)
계정을 날리니까 코멘트도 지워지는군요... 죄송.. ㅎ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Collection기반 인터페이스는 어떨까?
val db = MongoDB("HotFlow")
db('post) << Map( ("postId" -> 1212 ) , ( "post" -> "test") )
db('post) .find(Map("postId" -> 1)