[英]In Lift with MongoDB, storing lists of heterogeneous data
我需要制作一个可以包含对象列表的Web服务。 一个列表可以包含许多类型的对象。 例如,在这里,我有一个媒体项目库。 每个项目都可以是链接或视频,每个都有自己的元数据。
我想使用Lift Web框架来完成此操作,因为我需要将其编译为WAR,并且以前使用过Lift。
我认为使用MongoDB进行这种存储将有效,因为按照定义,它应该能够处理异构项目的集合。
我可以定义要存储在Lift记录中的BSON对象的类型,但似乎不能不放弃在一个记录/集合中仅创建一种类型的对象。 理想情况下,我希望库中的每个“事物”(因为缺少更好的词)都可以是视频或链接。 例如:
[
{
"type" : "Video",
"title" : "Story",
"videoID" : "123ab4",
"description": "Feature-length epic",
"time" : 12.6
},
{
"type" : "link",
"url" : "http://www.google.com",
"title": "Search for stuff"
}
]
我应该能够使用正确的继承类型来做到这一点,但是所有记录对象的父对象从该对象继承的方式都会使我失望。 我可以让它工作吗? 有Lift可以使用的不同东西的集合吗?
到目前为止,这就是我所拥有的。 我没有测试过,但即使它的工作原理是什么它是不是我想要的。
import net.liftweb.record._
import net.liftweb.record.field._
import net.liftweb.mongodb._
import net.liftweb.mongodb.record._
import net.liftweb.mongodb.record.field._
class VideoShelf private () extends BsonRecord[VideoShelf] {
def meta = VideoShelf
object title extends StringField (this, 256)
object videoID extends StringField (this, 32 )
object description extends StringField (this, 256)
object time extends DecimalField(this, 0 )
}
object VideoShelf extends VideoShelf with BsonMetaRecord[VideoShelf]
class LinkShelf private () extends BsonRecord[LinkShelf] {
def meta = LinkShelf
object url extends StringField(this, 128)
object title extends StringField(this, 256)
}
object LinkShelf extends LinkShelf with BsonMetaRecord[LinkShelf]
class MediaLibrary private () extends MongoRecord[MediaLibrary] with ObjectIdPk[MediaLibrary] {
def meta = MediaLibrary
///////////////////////////////////////
///////////////////////////////////////
// What I want is this record type to
// contain either of these:
///////////////////////////////////////
object videoshelf extends BsonRecordField(this, VideoShelf)
object linkshelf extends BsonRecordField(this, LinkShelf )
}
object MediaLibrary extends MediaLibrary with MongoMetaRecord[MediaLibrary]
我怎么能得到这个?
寻找更多信息后,我发现了这篇文章: https : //groups.google.com/forum/#!topic/liftweb/LmkhvDgrgrI
这使我得出了这个结论,我认为它是正确的,尽管尚未经过检验。 我可能会缺少一些东西,以使其无法正常工作。
import net.liftweb.record._
import net.liftweb.record.field._
import net.liftweb.mongodb._
import net.liftweb.mongodb.record._
import net.liftweb.mongodb.record.field._
/**
* The base record type for library objects.
*/
trait MediaLibrary[T <: MediaLibrary[T]] extends MongoRecord[T] with ObjectIdPk[T] {
self: T =>
}
/**
* Items in the library that are videos.
*/
class VideoItem extends MediaLibrary[VideoItem] {
def meta = VideoItem
object title extends StringField (this, 256)
object videoID extends StringField (this, 32 )
object description extends StringField (this, 256)
object time extends DecimalField(this, 0 )
}
object VideoItem extends VideoItem with MongoMetaRecord[VideoItem]
/**
* Items in the library that are links.
*/
class LinkItem extends MediaLibrary[LinkItem] {
def meta = LinkItem
object url extends StringField (this, 256)
object title extends StringField (this, 256)
}
object LinkItem extends LinkItem with MongoMetaRecord[LinkItem]
我还刚刚发现,有一个MongoDB特定的记录,其中包含案例类的列表。 那似乎正是我所需要的! 这是Scala和Mongo携手使用的强大功能! 那就是我从一开始就想要的。 我明天必须尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.