Search code examples
javamongodbmorphia

morphia: can I index on a referenced field?


I'm pretty the answer to this is no, but figured I would check.

If I have a record which references another record, like this:

  public class Account{
      @id
      ObjectId id;
      long balance;
      @Reference
      Customer customer;
  }

  public class Customer{
      @id
      ObjectId id;
      String name;
      Address address
      @indexed
      long social;
 }

can I create an index based off of the referenced value? for instance if I wish to be able to index my accounts by social, so I can look up all accounts for a given social quickly, can I do that?


Solution

  • If u wanted to do that u could use Compund Index

    http://docs.mongodb.org/manual/tutorial/create-a-compound-index/#index-create-compound-index

    in your case db.Account.ensureIndex( { customer.id: 1, customer.name: 1, .... } )