5

I have a document in collection as :

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

I am trying to perform following aggregation function in spring boot where i want to retrieve "errorSegments" of a particular hour that matches an agent , which is working fine in mongo shell :

Working 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"
    }
}])

So it provides output only , which is desired result :

{ "agentName" : "agentX" }

But my following code in spring gives error :

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);

Following is the Logs :

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 Answer 1

6

The source of error is the alias that you use with filter operation. It should be errorSegments instead of prop but you you other problems too. Use fieldname i.e $ prefix is not right.

Here is the updated aggregation. You can use $filter helper.

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);

Below is the generated query.

[
  {
    "$match": {
      "hourNumber": 21,
      "errorSegments.agentName": "agentX"
    }
  },
  {
    "$project": {
      "errorSegments": {
        "$filter": {
          "input": "$errorSegments",
          "as": "e",
          "cond": {
            "$eq": [
              "$$e.agentName",
              "agentX"
            ]
          }
        }
      }
    }
  },
  {
    "$unwind": "$errorSegments"
  },
  {
    "$replaceRoot": {
      "newRoot": "$errorSegments"
    }
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much , works like a charm . Can you suggest good sources for advanced operations like these ? because i wasn't able to find it in spring mongo documentation .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.