Morphia query with or operator

user1573690 picture user1573690 · Jul 26, 2013 · Viewed 10.3k times · Source

I want to know how to write a Morphia mongodb query with 'or' operator
I wrote mongodb query like this and this work fine

db.Inv.find({$or:[{sug_id:2},{grp_id:2}]})  

But i got confused when i try to write this in morphia, following query is wrong but how can write something similar to this

List<Inv> invs = ds.find(Inv.class).field("grp_id").hasAnyOf(grpId).or(field("sug_id")).hasAnyOf(grpId).asList();  

Thanks

Answer

evanchooly picture evanchooly · Jul 26, 2013

not sure why hasAnyOf() is in there but try this:

Query<Inv> query = ds.find(Inv.class);            
query.or(
  query.criteria("grp_id").equal(2),
  query.criteria("sug_id").equal(2));
List<Inv> invs = query.asList();