java.lang.IllegalArgumentException:在MongoDB中执行聚合的引用无效

时间:2018-03-22 14:23:06

标签: java spring mongodb aggregation-framework

我在集合中有一个文件:

{
    "_id": ObjectId("5ab273ed31fa764560a912f8"),
    "hourNumber": 21,
    "errorSegments": [{
        "agentName": "agentX"
    },
    {
        "agentName": "agentY"
    }]
}

我正在尝试在spring boot中执行以下聚合函数,我想要检索" errorSegments"与代理匹配的特定小时,在mongo shell中正常工作:

工作shell:

db.errorsegment.aggregate([{
    "$match": {
        "hourNumber": 21,
        "errorSegments.agentName": "agentX"
    }
},
{
    "$project": {
        "errorSegments": {
            "$filter": {
                "input": "$errorSegments",
                "as": "e",
                "cond": {
                    "$eq": ["$$e.agentName",
                    "agentX"]
                }
            }
        }
    }
},
{
    "$unwind": "$errorSegments"
},
{
    "$replaceRoot": {
        "newRoot": "$errorSegments"
    }
}])

因此它仅提供输出,这是期望的结果:

{ "agentName" : "agentX" }

但是我在spring中的以下代码会出错:

MatchOperation match = Aggregation.match(Criteria.where("hourNumber").is(21).and("errorSegments.agentName").is("agentX"));

        ProjectionOperation project = Aggregation.project()
                  .and(new AggregationExpression() {

                      @Override
                        public DBObject toDbObject(AggregationOperationContext context) {

                          DBObject filterExpression = new BasicDBObject();
                          filterExpression.put("input", "$errorSegments");
                          filterExpression.put("as", "e");
                          filterExpression.put("cond", new BasicDBObject("$eq", Arrays.<Object> asList("$$e.agentName","agentX")));

                          return new BasicDBObject("$filter", filterExpression);
                      } }).as("prop");


        UnwindOperation unwind = Aggregation.unwind("$errorSegments");

        ReplaceRootOperation replaceRoot = Aggregation.replaceRoot("$errorSegments");

        Aggregation aggregation = Aggregation.newAggregation(match,project,unwind,replaceRoot);

        AggregationResults<ErrorSegment> errorSegments = mongoOps.aggregate(aggregation, SegmentAudit.class , ErrorSegment.class);

以下是日志:

java.lang.IllegalArgumentException: Invalid reference 'errorSegments'!
    at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:99) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:71) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.aggregation.UnwindOperation.toDBObject(UnwindOperation.java:95) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.aggregation.AggregationOperationRenderer.toDBObject(AggregationOperationRenderer.java:56) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.aggregation.Aggregation.toDbObject(Aggregation.java:580) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1567) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1502) ~[spring-data-mongodb-1.10.3.RELEASE.jar:na]

1 个答案:

答案 0 :(得分:2)

错误源是您使用过滤器操作的别名。它应该是errorSegments而不是prop,但你也有其他问题。使用fieldname即$ prefix不对。

这是更新的聚合。您可以使用$filter帮助程序。

MatchOperation match = Aggregation.match(Criteria.where("hourNumber").is(21).and("errorSegments.agentName").is("agentX"));
ProjectionOperation project = Aggregation.
            project().and(ArrayOperators.Filter.filter("errorSegments")
                    .as("e")
                    .by(ComparisonOperators.Eq.valueOf(
                            "e.agentName")
                            .equalToValue(
                                    "agentX")))
                    .as("errorSegments");
UnwindOperation unwind = Aggregation.unwind("errorSegments");
ReplaceRootOperation replaceRoot = Aggregation.replaceRoot("errorSegments");
Aggregation aggregation = Aggregation.newAggregation(match,project,unwind,replaceRoot);

以下是生成的查询。

[
  {
    "$match": {
      "hourNumber": 21,
      "errorSegments.agentName": "agentX"
    }
  },
  {
    "$project": {
      "errorSegments": {
        "$filter": {
          "input": "$errorSegments",
          "as": "e",
          "cond": {
            "$eq": [
              "$$e.agentName",
              "agentX"
            ]
          }
        }
      }
    }
  },
  {
    "$unwind": "$errorSegments"
  },
  {
    "$replaceRoot": {
      "newRoot": "$errorSegments"
    }
  }
]