嵌入式列表中的Morphia查询

时间:2016-11-04 15:46:56

标签: java mongodb morphia

我的问题是查询复杂元素。

@Entity("Activity")
public class Activity extends BaseEntity {
        private String name;
        private String description;
        private Date deadline;
        private Boolean completed;
        @Embedded
        private List<SubActivity> listSubActivity;
   .
   .
   .
}

其中

@Entity("SubActivity")
public class SubActivity extends BaseEntity {
    private String name;
    private Date deadline;
    private Boolean completed;
.
.
.
}

我必须找到子活动已过期的所有活动。 我试试

Query<Activity> queryA = datastore.createQuery(Activity.class);
queryA.and(
   queryA.criteria("deadline").lessThan(today), // where today is a Date()
   queryA.criteria("completed").equal(false)
 );

但它不起作用,因为还检索此活动

{
  "_id" : ObjectId("581352597dfd063decb0b357"),
  "className" : "com.etherweaver.collections.Activity",
  "name" : "Name",
  "description" : "description",
  "deadline" : ISODate("2016-11-02T23:00:00.000Z"),
  "completed" : true,
  "listSubActivity" : [{
      "name" : "sub activity 1",
      "deadline" : ISODate("2016-11-01T23:00:00.000Z"), 
      "completed" : true 
    }, {
      "name" : "sub activity 2",
      "deadline" : ISODate("2016-11-06T23:00:00.000Z"),
      "completed" : false
    }, {
      "name" : "sub activity 3",
      "deadline" : ISODate("2016-11-06T23:00:00.000Z"),
      "completed" : true
    }]
}

子活动1已过期但已完成。

是否有一种方法可以查询嵌入了逐行验证条件的列表?

1 个答案:

答案 0 :(得分:3)

您可以尝试以下内容。

Query<SubActivity> query = datastore.createQuery(SubActivity.class)
        .filter("deadline < ", new Date())
        .filter("completed = ", true);

List<Activity> activities = datastore.find(Activity.class)
                                              .field("listSubActivity")
                                              .elemMatch(query)
                                              .asList();   

<强>更新

要使用hasThisElement,它只允许等于比较而不是其他条件比较。

所以我不确定这对你有用,但很抱歉,我现在还没有其他任何办法。

SubActivity filter = new SubActivity();
filter.setDeadline(new Date());
filter.setCompleted(true);

List<Activity> activities = datastore.find(Activity.class)
                                              .field("listSubActivity")
                                              .hasThisElement(filter)
                                              .asList();   

更新2

基于聚合的方法 - 此方法将根据查询过滤行。

import static org.mongodb.morphia.aggregation.Group.*;

Query<Activity> query = datastore.createQuery(Activity.class)
               .filter("listSubActivity.deadline < ", anyDate)
               .filter("listSubActivity.completed = ", true);

Iterator<Activity> activities = datastore.createAggregation(Activity.class).
               unwind("listSubActivity").
               match(query).
               group("_id", grouping("className", first("className")),
                       grouping("name", first("name")),
                       grouping("description", first("description")),
                       grouping("deadline", first("deadline")),
                       grouping("completed", first("completed")), grouping("listSubActivity", push("listSubActivity"))).aggregate(Activity.class);