$filter inside $project MongoDB Using Spring Data

前端 未结 2 1944
天命终不由人
天命终不由人 2020-12-20 03:40

I have a subdocument that is an array of a parent document. \"devices\"

In that array I\'ve got a property which is a Date property.

I want

2条回答
  •  感动是毒
    2020-12-20 04:07

    I managed to solve my problem with the Spring boot Version 1.4.1.RELEASE and I did this:

    Aggregation aggregation = newAggregation(
                match(Criteria.where("devices.evaluationDate").is(date)),
                project().and(new AggregationExpression() {
                    @Override
                    public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                        DBObject filterExpression = new BasicDBObject();
                        filterExpression.put("input", "$devices");
                        filterExpression.put("as", "device");
                        filterExpression.put("cond", new BasicDBObject("$eq", Arrays. asList("$$device.evaluationDate", date)));
                        return new BasicDBObject("$filter", filterExpression);
                    }
                }).as("devices")
        );
    
        AggregationResults list = mongoOperations.aggregate(aggregation,
                MyClass.class, MyClass.class);
    
    
    

    I elaborated based on this: Does Spring Data MongoDb support $filter array aggregations operator?

    My project was on Spring boot 1.4.0.RELEASE but that version didnt have the AggregationExpression interface PUBLIC, so i just updated to 1.4.1.RELEASE and i did work.

    提交回复
    热议问题