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" whenUser
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