java - Morphia 中的复杂 AND-OR 查询

标签 java mongodb morphia

我一直在尝试组合 Query 接口(interface)的 and() 和 or() 方法来创建一组条件,其中有 2 个条件列表,并且每个条件中至少有一个必须满足。

我读了this discussion并一直在尝试使用 Query.and() 来组合我的两个 $or 子句。

本质上,我想说的是:

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() 两次一样。

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

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

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

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

我看不到太多文档,但查看测试用例,这似乎是解决此问题的正确方法。任何人都可以帮助阐明为什么这不像我预期的那样工作吗?

(这个问题是 cross-posted to the Morphia mailing list ,因为我不确定哪个地方会得到最好的回应)

编辑 0:

更新:重新审视这个问题,我检查了 Morphia 测试代码,一切运行良好,但我无法在测试代码中重现我的问题。

因此,我创建了一个新项目来尝试获取我想要的查询的工作示例。但是,即使是准系统测试项目,我也遇到了同样的问题。

项目已mavenised,POM为:

<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 类:

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 查询,但我不明白为什么

最佳答案

只是猜测(没时间测试),不过应该是:

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

更新 你是对的,Query.criteria 不对——它是在简单测试中使用的,所以我认为你错过了什么。这似乎对我有用(将其分为两个语句):

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

更新2更完整的测试代码:

Criteria[] arrayA = {dao.createQuery().criteria("test").equal(1), dao.createQuery().criteria("test").equal(3)};
Criteria[] arrayB = {dao.createQuery().criteria("test").equal(2), dao.createQuery().criteria("test").equal(4)};;
Query q = dao.createQuery();
q.and(
  q.or(arrayA),
  q.or(arrayB)
);
System.out.println(q.toString());

给出:

{ "$and" : [ { "$or" : [ { "test" : 1} , { "test" : 3}]} , { "$or" : [ { "test" : 2} , { "test" : 4}]}]}

关于java - Morphia 中的复杂 AND-OR 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10284116/

相关文章:

java - 请推荐一种在Spring MVC 3应用中使用的 View 技术

java - 使用 MongoDB 和 Morphia 编译 GWT 应用程序

java - MongoDB如何使用morphia加载引用文档的两个属性

java - 音频格式转换

ISO-8859-9 的 java xml 解析

java - 我想,试图获得重心位置?

ruby-on-rails - 具有可能不同类型 mongoid 的字段

javascript - mongodb中如何按多个字段聚合

node.js - 函数返回对象作为promise {pending}

java - 具有未定义字段数的 Morphia 更新文档