将 Salat 与 MongoDB 一起使用时,处理复合键的最佳方法是什么? [英] What is the best way to deal with compsite keys when using Salat with MongoDB?
问题描述
我正在将 Salat 与 MongoDB 一起使用,并且我正在尝试转换为自然键以避免数据库中的重复.我使用的案例类看起来有点像:
I'm using Salat with MongoDB and I'm trying to convert to natural keys to avoid duplicates in the database. The case class I'm using looks somewhat like:
case class Foo(someRelatedId: String, email: String ...)
我想添加一个由 someRelatedId+email 组成的自然键,并让 MongoDB 使用它而不是默认的 ObjectId.从文档中我觉得这是可能的,但我仍在摸索一个可行的解决方案.这在很大程度上是由于我对 Scala 本身缺乏熟练程度,我敢肯定.
I would like to add a natural key that consists of someRelatedId+email and have MongoDB use that instead of the default ObjectId. From the documentation I get the feeling it is possible, but I'm still groping around for a working solution. This is in a large part due to my lack of proficiency with Scala itself, I'm sure.
更新:我现在有一个可行的解决方案,但我仍然想知道这是否是最好的方法
Update: I have a working solution now, but I'm still wondering if it is the best way to go
case class Foo(someRelatedId: String, email: String, naturalKey: String)
object Foo {
def apply((someRelatedId: String, email: String) {
apply(someRelatedId, email, someRelatedId+email)
}
}
然后在 package.scala 中,我映射到 自定义 Salat 上下文::>
And then in package.scala I map to a custom salat context:
implicit val ctx = new Context() {
val name = Some("Custom Context")
}
ctx.registerGlobalKeyOverride(remapThis = "naturalKey", toThisInstead = "_id")
这样我就可以避免在我的域类中使用强制(无意义)_id 字段,但我必须在伴随对象上重载 apply(),这看起来有点笨拙.
This way I avoid having a mandatory (meaningless) _id field in my domain classes, but I do have to overload apply() on the companion object, which seems a bit clunky.
推荐答案
Salat 主要开发人员在这里.
main Salat developer here.
按照 Milan 的建议,为您的复合键创建一个案例类:
Like Milan suggested, create a case class for your composite key:
case class FooKey(someRelatedId: String, email: String)
case class Foo(@Key("_id") naturalKey: FooKey) {
// use @Persist if you want these fields serialized verbatim to Mongo - see https://github.com/novus/salat/wiki/Annotations for details
@Persist val email = naturalKey.email
@Persist val someRelatedId = naturalKey.someRelatedId
}
object FooDAO extends SalatDAO[Foo, FooKey](collection = /* some Mongo coll */ )
如果您反对将_id"作为字段名称,您可以在上下文中使用全局覆盖将_id"重新映射到naturalKey",或者在每个对象上提供特别的@Key 覆盖.
If you object to "_id" as a field name, you can use a global override in the context to remap "_id" to "naturalKey", or supply ad hoc @Key overrides on each object.
我个人不喜欢在你的模型中给 _id 一个不同的名字,因为你的 Mongo 查询必须使用序列化键_id",而你的所有业务逻辑都必须使用案例类字段名称(naturalKey"或其他),但 YMMV.
I don't personally like giving the _id a different name in your models as then your Mongo queries must use the serialized key "_id" while all your business logic must use the case class field name ("naturalKey" or whatever), but YMMV.
附言我们的邮件列表位于 http://groups.google.com/group/scala-salat - 我会在那里比 Stack Overflow 更快地看到您的问题.
P.S. Our mailing list is at http://groups.google.com/group/scala-salat - I'll see your question quicker there than Stack Overflow.
这篇关于将 Salat 与 MongoDB 一起使用时,处理复合键的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!