Archives Lift与MongoDB
Post
Cancel

Lift与MongoDB

越来越多的公司和组织掺和到NoSQL运动里来,我也跟风玩玩儿MongoDB。

Lift对MongoDB已经有了很好的支持:http://www.assembla.com/wiki/show/liftweb/MongoDB

下面这段例子是一个非常简单的model设计:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class ShortenedUrl extends MongoRecord[ShortenedUrl] with MongoId[ShortenedUrl] {
  def meta = ShortenedUrl
 
  object linkId extends StringField(this, 10)
 
  object originUrl extends StringField(this, 500)
 
  object shortUrl extends StringField(this, 100)
 
  object date extends DateField(this)
 
  object ip extends StringField(this, 15)
 
  object clickCount extends IntField(this)
}
 
object ShortenedUrl extends ShortenedUrl with MongoMetaRecord[ShortenedUrl]
 
class NextId extends MongoRecord[NextId] with MongoId[NextId] {
  def meta = NextId
 
  object next extends StringField(this, 10)
}
 
object NextId extends NextId with MongoMetaRecord[NextId]

这个东西是我仿照yourls做的数据模型。很简单,两个collection,一个记录缩短域名的所有信息,一个记录下一个URL的id是什么。

查找:

1
ShortenedUrl.find(ShortenedUrl.linkId.name -> id)

存储:

1
2
currentShortenedUrl.linkId(linkId).shortUrl(Props.get("site").open_! + "/" + linkId).
                ip(containerRequest.open_!.remoteAddress).clickCount().save

根本不用描述,太简单了。

This post is licensed under CC BY 4.0 by the author.