Morphia query with or operator

淺唱寂寞╮ 提交于 2019-12-23 07:45:58

问题


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


回答1:


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();


来源:https://stackoverflow.com/questions/17873969/morphia-query-with-or-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!