Pergunta

I've just read this post on how to use composite keys with Salat and MongoDB... and it is more or less clear how it works. Nevertheless, I'm wondering if the @Key annotation is mandatory even if my key is already named id. I'm trying to serialize a few case classes coming from an external library and I'd prefer not to redefine them. Given the following external case classes:

case class UserId(id: String, mail: String)
case class User(id: UserId, name: String, surname: String)

Would it be possible to serialize an User object like this (i.e. without the @Key annotation)?

object UserDAO extends SalatDAO[User, UserId](...)

Thanks.

Foi útil?

Solução

MongoDB expects every document in a collection to have an id field called "_id". If you fail to provide a "_id" field, then the mongo-java-driver will create one for you.

So what you need to do is either:

  • name the field "_id", not "id"
  • use @Key annotation to remap your "id" field to "_id" when User is serialized
  • add a global key remapping of "id" to "_id" so that it is not necessary to use @Key in place

When dealing with an external model, option three is the most efficient. See the section "Global Key Remapping" in this wiki article: https://github.com/novus/salat/wiki/CustomContext

Once you've made this remapping, UserDAO should work fine with the composite key.

Remember that even if your field is named "id" in the User case class, queries must refer to _id because this is what the field is called in your MongoDB document.

Also, remember that when you use a composite key you're going to need to create a custom index that covers all the fields of your composite key in the order for which you are going to query for them. You can use Dex to analyse your MongoDB log and make recommendations: https://github.com/mongolab/dex

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top