Mongodb吗非亚聚集

Mongodb Morphia aggregation

我在 Morphia 中创建聚合时遇到问题,documentation 真的不清楚。这是原始查询:

db.collection('events').aggregate([
  {
    $match: {
      "identifier": {
        $in: [
          userId1, userId2
        ]
      },
      $or: [
        {
          "info.name": "messageType",
          "info.value": "Push",
          "timestamp": {
            $gte: newDate("2015-04-27T19:53:13.912Z"),
            $lte: newDate("2015-08-27T19:53:13.912Z")
          }
        }
      ]
    }{
      $unwind: "$info"
    },
    {
      $match: {
        $or: [
          {
            "info.name": "messageType",
            "info.value": "Push"
          }
        ]
      }
    ]);

他们文档中的唯一示例是使用 out 并且有一些示例 here 但我无法使其工作。

我什至没有通过第一场比赛,这就是我的情况:

    ArrayList<String> ids = new ArrayList<>();
            ids.add("199941");
            ids.add("199951");
    Query<Event> q = ads.getQueryFactory().createQuery(ads);
    q.and(q.criteria("identifier").in(ids));
    AggregationPipeline pipeline = ads.createAggregation(Event.class).match(q);
Iterator<Event> iterator =  pipeline.aggregate(Event.class);

一些帮助或指导以及如何开始查询或它如何工作会很好。

您需要通过将代码分解为易于理解的可管理部分来为 match() 管道创建查询。那么让我们开始吧 使用匹配标识符字段的查询,到目前为止你已经做得很好了。然后我们需要结合查询的 $or 部分。

从您离开的地方继续,将完整查询创建为:

Query<Event> q = ads.getQueryFactory().createQuery(ads);
Criteria[] arrayA = {
    q.criteria("info.name").equal("messageType"), 
    q.criteria("info.value").equal("Push"),
    q.field("timestamp").greaterThan(start);
    q.field("timestamp").lessThan(end);
};

Criteria[] arrayB = {
    q.criteria("info.name").equal("messageType"), 
    q.criteria("info.value").equal("Push")  
};

q.and(
    q.criteria("identifier").in(ids),
    q.or(arrayA)
);

Query<Event> query = ads.getQueryFactory().createQuery(ads);
query.or(arrayB);

AggregationPipeline pipeline = ads.createAggregation(Event.class)
                                  .match(q)
                                  .unwind("info")
                                  .match(query);
Iterator<Event> iterator =  pipeline.aggregate(Event.class);

以上内容未经测试,但会引导您到离家更近的地方,因此请酌情进行一些必要的调整。对于一些参考,以下 SO 问题可能会给你一些指示:

  1. Complex AND-OR query in Morphia
  2. Morphia query with or operator

当然还有 AggregationTest.javaGithub 页面