如何通过 Casbah 将带有列表的 scala 对象转换为 MongoDBObject
我正在通过编写一个简单的应用程序来学习 MongoDB 和 Casbah。当我尝试将带有列表成员的对象转换为 MongoDB 对象时遇到了困难。这是我的类
case class BorrowerRecord( name: String, checkedOut: List[BookTag]) {
require(!name.isEmpty)
require(!checkedOut.isEmpty)
}
case class BookTag (subject: Subject, bookName: String) {
require(!bookName.isEmpty)
}
case class Subject (name: String, category: Category) {
require(!name.isEmpty)
}
Category 是一个密封特征,具有 2 个案例类实现,我打算像“Enum”一样使用它,
sealed trait Category {
def name: String
}
object Category {
case object Computing extends Category { val name = "Computing"}
case object Math extends Category { val name = "Math"}
}
因此,BorrowerRecord 的实例将保留一个人从图书馆借出的书籍,每本书都由 BookTag 对象标识。 BookTag 保存有关书籍的一些信息,如书名、主题名称、类别等。
假设我有一个 BorrowerRecord 并希望将其保存到 MongoDB
val borrowOnToday = BorrowerRecord( "My Name", List( BookTag(Subject("J2EE", Category.Computing), "Head First Java"),
BookTag(Subject("Linear Algebra", Category.Math), "Algebra for Dummies")))
我应该如何使用 Casbah 将其转换为 MongoDBObject ?
或者 Casbah 不是可行的方法,还有其他库可以帮助我更轻松地将其持久保存到 MongoDB 中?
I'm learning MongoDB and Casbah by writing a simple app. Got stuck when I try to convert an object with a list member into a MongoDB Object. Here is my class
case class BorrowerRecord( name: String, checkedOut: List[BookTag]) {
require(!name.isEmpty)
require(!checkedOut.isEmpty)
}
case class BookTag (subject: Subject, bookName: String) {
require(!bookName.isEmpty)
}
case class Subject (name: String, category: Category) {
require(!name.isEmpty)
}
Category is a sealed trait with 2 case class implementation, I intended to use this like "Enum"
sealed trait Category {
def name: String
}
object Category {
case object Computing extends Category { val name = "Computing"}
case object Math extends Category { val name = "Math"}
}
So, a instance of BorrowerRecord will keep what books a person checked out from the library, each book is identified by a BookTag object. A BookTag keeps some information about a book like bookname, subject name, Category, etc.
Lets say I've a BorrowerRecord and want to save it to MongoDB
val borrowOnToday = BorrowerRecord( "My Name", List( BookTag(Subject("J2EE", Category.Computing), "Head First Java"),
BookTag(Subject("Linear Algebra", Category.Math), "Algebra for Dummies")))
How should I convert this to MongoDBObject using Casbah ?
Or Casbah is not the way to go and there're other libraries that can help me persist this into MongoDB more easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用案例类,请使用 salat(按 <- 和 -> 在演示文稿中移动) 。
这非常简单:
通常,我同时使用它们:casbah 来查询 Mongo 或从 Mongo 查询,salat 来转换为案例类,反之亦然。
是的,salat 支持带有列表的案例类(此处是支持的集合的列表)。
To work with case classes use the salat (press <- and -> to move through the presentation).
It is pretty simple:
Usually, I'm using them both: casbah to querying to/from Mongo, and salat to make a conversions to case classes and vice versa.
And yes, salat supports case classes with Lists (here is the list of supported collections).
我使用我自己的库 Subset (我最近将其开源)以及 MongoDB Java 驱动程序。与 Salat 不同,它是显式的,您必须声明所有序列化代码,尽管 Subset 有助于使其变得非常简单。作为奖励,您将能够创建查询。
对于您的数据模型,代码可能看起来像
Subset 知道如何序列化
List[T]
,但它需要一个隐式ValueWriter[BookTag]
:我希望您明白继续使用
Subject
和Category
的想法I use my own library Subset (I've open-sourced it recently) along with MongoDB Java driver. Unlike Salat it's explicit, you have to declare all the serialization code, though Subset helps keep it quite simple. You'll get ability to create queries as a bonus.
For your data model, the code may look like
Subset knows how to serialize
List[T]
, but it needs an implicitValueWriter[BookTag]
for that:I hope you got the idea to continue with
Subject
andCategory