我想知道如何使用'或'运算符编写Morphia mongodb查询
我写了这样的mongodb查询这个工作正常
db.Inv.find({$or:[{sug_id:2},{grp_id:2}]})
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在morphia中写这个时,我感到很困惑,以下查询是错误的,但是如何写出与此类似的东西
List<Inv> invs = ds.find(Inv.class).field("grp_id").hasAnyOf(grpId).or(field("sug_id")).hasAnyOf(grpId).asList();
Run Code Online (Sandbox Code Playgroud)
谢谢
我一直在尝试组合Query接口的and()和or()方法来创建一组条件,其中有2个条件列表,并且必须满足每个条件中的至少一个条件.
我阅读了这个讨论,并一直在尝试使用Query.and()来组合我的两个$或子句.
基本上,我想说:
Criteria[] arrayA;
Criteria[] arrayB;
// Programatically populate both arrays
Query q = dao.createQuery().and(
q.or(arrayA),
q.or(arrayB)
);
Run Code Online (Sandbox Code Playgroud)
我正在使用标准数组,因为我必须遍历几个不同的输入以生成我需要的特定条件,这种方法适用于我只使用单个$或者,但我不能让Morphia生成查询我希望当我尝试在$中包含$或$时,如上所述.我发现没有$和查询以及第二个$或者已经覆盖了第一个$,就像我只是简单地调用或()两次一样.
例如,我希望生成一个查询,如下所示:
{
"$and": {
"0": {
"$or": {
"0": //Some criteria,
"1": //Some criteria,
"2": //Some criteria,
}
},
"1": {
"$or": {
"0": //Some other criteria,
"1": //Some other criteria,
"2": //Some other criteria,
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我只是得到这样的查询:
{
"$or": {
"0": //Some other criteria,
"1": //Some other criteria,
"2": //Some other criteria,
}
}
Run Code Online (Sandbox Code Playgroud)
我看不到很多文档,但看看测试用例,这似乎是解决这个问题的正确方法.任何人都可以帮助解释为什么这不符合我的预期吗?
(这个问题被 …