Java MongoDB,2个查询的公共部分

Java MongoDB,2个查询的公共部分,java,spring,mongodb,spring-mongodb,Java,Spring,Mongodb,Spring Mongodb,我有一份像这样的文件 @Document(collection="myDocument") public class MyDocument { @Id private String id; private List<Dates> dates; } public class Dates{ private String key; private DateTime value; } Criteria criteria = Criter

我有一份像这样的文件

@Document(collection="myDocument")
public class MyDocument {
    @Id
    private String id;

    private List<Dates> dates;
}

public class Dates{
    private String key;     
    private DateTime value;
}
Criteria criteria = Criteria.where("myDocument.dates.value")
    .exists(true)
    .gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00"))  //just converting String to DateTime here      
    .and("myDocument.dates.name")
    .exists(true)
    .all("Birthday"));
第二点:

 Criteria criteria = Criteria.where("myDocument.dates.value")
     .exists(true)
     .lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))  
     .and("myDocument.dates.name")
     .exists(true)
     .all("Mather's birthday"));
问题是,我不能将这两个
条件
放在一个
查询
中,这将导致错误。到目前为止,我找到的唯一解决方法是在这种情况下进行两个单独的
查询
,然后使用
resultA.retainal(resultB)

但问题是,我不想,这个数据库将存储大量的数据,这些请求将非常频繁。我需要它来快速工作,而在纯Java中合并两个列表将非常慢,因为数据量太大。你知道怎么处理吗

编辑# 下面是当我尝试在一个
查询中组合2个
条件时抛出的错误

捕获:(java.lang.RuntimeException),msg(json无法序列化类型: 类org.joda.time.DateTime)java.lang.RuntimeException:json不能 序列化类型:class org.joda.time.DateTime


您可以使用下面的代码
$和
一起执行查询,并使用
$elemMatch
在多个条件下匹配日期字段

差不多

Criteria criteria1 = Criteria.where("dates").
    elemMatch(
      Criteria.where("value").exists(true).gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00"))
      .and("name").exists(true).all("Birthday")
);
Criteria criteria2 = Criteria.where("dates").
    elemMatch(
      Criteria.where("value").exists(true).lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
       .and("name").exists(true).all("Mather's birthday")
);
Criteria criteria = new Criteria().andOperator(criteria1, criteria2);

注意:joda时间转换可能仍然存在问题。

能否从您的收藏中添加示例文档?名称字段是如何映射的?重要的部分非常简单
myDocument:{dates:[{name:“date1”,value“2000-01-01 00:00:00+0000”},{name:“date2”,value“2000-01-01 00:00:00+0000”},{name:“dateX”,value“2000-01-01 00:00:00+0000”},}
日期列表可以包含不同数量的元素,一个文档包含30个日期,另一个文档包含3个日期。名称值也可以不同。