2

I have following entity:

class Linf {
     @Id
     ObjectId id;
     @Reference
     Denied denied;
}

I want to find all Linfs that have Denied object with certain id. How can I do this? Will this query employ indexes? I want to avoid full scan if possible.

Thanx.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206

2 Answers2

1

If you don't have an index on "denied" it'll be a full collection scan either way but something like this should do it for you:

datastore.createQuery(Linf.class).field("denied").equal(new Key<Denied>(Denied.class, id)).fetch()
evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • 1
    I get WARNING: The type(s) for the query/update may be inconsistent; using an instance of type 'com.github.jmkgreen.morphia.Key' for the field 'org.kriyak.orm.Linf.denied' which is declared as 'org.kriyak.orm.Denied' – Stepan Yakovenko Dec 20 '13 at 15:56
  • That's fixed, I believe, in the official version. James's fork has been essentially shut down in favor of the official version. Please update to the other version and see if that persists. It's just a warning in any case but I believe I fixed that. If it's not in 0.105 it'll be in 0.106 which I'll release after the new year. – evanchooly Dec 20 '13 at 19:45
1

This works for me:

    Denied d2 = new Denied();
    d2.id = new ObjectId("52b4709f423d856472c34fa1");

    List list = datastore
            .createQuery(Linf.class)
            .field("denied")
            .equal(d2).asList();
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206