本文介绍了Morphia中的复杂AND-OR查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图结合Query接口的and()和or()方法来创建一组条件,其中包含2个条件列表,并且每个条件中至少一个要满足.

I've been trying to combine the and() and or() methods of the Query interface to create a set of conditions where there are 2 lists of criteria, and at least one from each must be satisfied.

我阅读了此讨论,并一直在尝试使用查询.and()合并我的两个$ or子句.

I read this discussion and have been trying to use the Query.and() to combine my two $or clauses.

基本上,我想说的是:

Criteria[] arrayA;
Criteria[] arrayB;

// Programatically populate both arrays

Query q = dao.createQuery().and(
    q.or(arrayA),
    q.or(arrayB)
);

我使用的是条件数组,因为我必须遍历几个不同的输入以生成所需的特定条件,并且这种方法在我仅使用单个$ or时有效,但是我无法让Morphia进行操作当我尝试在$ and中同时包含$ or子句时,生成我期望的查询,如我上面解释的那样.我发现没有$ and查询,第二个$ or覆盖了第一个,就像我只是简单地两次调用过or()一样.

I'm using arrays of criteria because I have to loop through several different inputs to generate the particular criteria I need, and this approach works when I'm just using a single $or, but I can't get Morphia to generate the query I expect when I try and include both $or clauses in the $and as I explained above. I find that there's no $and query and the second $or has overwritten the first, just as if I had simply called or() twice.

例如,我希望这样生成查询:

E.g I expect a query to be generated like this:

{
    "$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,
        }
    }
}

但是,我只收到这样的查询:

However, I just get a query like this:

{
    "$or": {
        "0": //Some other criteria,
        "1": //Some other criteria,
        "2": //Some other criteria,
    }
}

我看不到太多文档,但是看一下测试用例,这似乎是解决此问题的正确方法.任何人都可以帮忙弄清楚为什么它不能按我预期的那样工作吗?

I can't see much documentation, but looking at the test case, this seems to be the right way to go about this. Can anyone help shed any light on why this isn't working as I expect?

(此问题是交叉张贴到Morphia邮件列表中,因为我不确定哪个地方会获得最佳响应)

(This question was cross-posted to the Morphia mailing list, as I'm not sure which place would get the best response)

修改0 :

更新:重新审视这一点,我已经检查了Morphia测试代码,并且一切运行正常,我无法在测试代码中重现我的问题.

Update: Revisiting this, I have checked out the Morphia test code and every thing runs fine, I've been unable to reproduce my issue in the test code.

因此,我创建了一个新项目,以尝试获取所需查询的可行示例.但是,即使是准系统测试项目,我也遇到了同样的问题.

Therefore, I created a new project to try and get a working example of the query I want. However I encountered the same issue, even with a barebones test project.

该项目已被破坏,而POM为:

The project is mavenised, and the POM is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Test</name>


<dependencies>
    <dependency>
        <groupId>com.google.code.morphia</groupId>
        <artifactId>morphia</artifactId>
        <version>0.99</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <!-- Force the use of the latest java mongoDB driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

</project>

我有一个TestEntity类:

I have a TestEntity class:

import java.util.Map;

import com.google.code.morphia.annotations.Entity;

@Entity
public class TestEntity {
    Map<String, Integer> map;
}

最后是我的测试班:

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.QueryImpl;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class Test {

    static Mongo mongo;
    static Morphia m;
    static Datastore ds;

    static {
        mongo = null;
        try {
            mongo = new Mongo();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
        m = new Morphia();
        ds = m.createDatastore(mongo, "test");
    }

    public static void main(String[] args) {
        populate();
        query();
    }

    public static void query() {
        Query<TestEntity> q = ds.createQuery(TestEntity.class);

        q.and(q.or(q.criteria("map.field1").exists()),
                q.or(q.criteria("map.field2").exists()));

        Iterable<TestEntity> i = q.fetch();
        for (TestEntity e : i) {
            System.out.println("Result= " + e.map);
        }

        QueryImpl<TestEntity> qi = (QueryImpl<TestEntity>) q;
        System.out    
                .println("Query= " +         qi.prepareCursor().getQuery().toString());
    }

    public static void populate() {
        TestEntity e = new TestEntity();
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("field1", 1);
        map.put("field2", 2);
        e.map = map;

        ds.save(e);
    }
}

对我来说,上面的代码无法产生正确的$ and查询,但我看不出为什么

For me, the above code doesn't produce the correct $and query, but I can't see why

推荐答案

尽管Morphia 0.99包括了Query.and(Criteria ...)方法,但它不会生成正确的查询.

Despite Morphia 0.99 including the Query.and(Criteria ...) method, it doesn't generate the correct query.

问题338 ,该问题解决了对显式 $ and 子句以Morphia 0.99.1为目标,Morphia 0.99.1目前仅可通过Maven作为SNAPSHOT版本使用.

Issue 338, which addresses support for explicit $and clauses in queries is targeted at Morphia 0.99.1, which is currently only available as a SNAPSHOT version via Maven.

但是,使用0.99.1-SNAPSHOT为我们解决了这个问题.

However, using 0.99.1-SNAPSHOT resolved the issue for us.

这篇关于Morphia中的复杂AND-OR查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:38