복붙노트

[SPRING] Spring Data MongoDb는 $ filter 배열 집계 연산자를 지원합니까?

SPRING

Spring Data MongoDb는 $ filter 배열 집계 연산자를 지원합니까?

MongoTemplate을 사용하여 Spring 데이터에서 다음 작업용 mongoDb 쿼리를 구현하려고합니다.

db.answers.aggregate([
        { "$match" : { "object_id" : "1" } },
        { "$project": { 'answer_list': 1, 'profile': { $filter : { input: '$answer_list', as: 'answer', cond: { $eq: [ '$$answer.question', 2 ] } } } } },
        { "$unwind" : "$profile"},
        { "$unwind" : "$answer_list"},
        { "$group" : { "_id" : { "question" : "$answer_list.question", "answer" : "$answer_list.answer", "criteria" : "$profile.answer"}, "count" : { "$sum" : 1 } } },
        { "$sort" : { "_id.question" : 1, "_id.answer" : 1 } }
]);

컬렉션에는 다음과 같은 구조가 있습니다.

{
"_id" : ObjectId("..."),
"object_id" : ObjectId("..."),
"answer_list" : [ 
    {
        "question" : NumberLong(0),
        "answer" : NumberLong(0)
    }, 
    {
        "question" : NumberLong(1),
        "answer" : NumberLong(2)
    }, 
    {
        "question" : NumberLong(2),
        "answer" : NumberLong(2)
    }
]}

제가 여기서하려고하는 것은 간단한 설문 조사 제출 데이터에 대한 보고서입니다. 질문은 "첫 번째 질문에 0으로 대답 한 사용자가 두 번째 질문에 어떻게 대답 했습니까?" 나는 하루 종일 SpringData Mongo Db 문서를 검색하는 데 보냈지 만 아무것도 발견하지 못했습니다. 누구든지 도와 줄 수 있습니까?

티아

해결법

  1. ==============================

    1.자신 만의 AggregationExpression을 제공하여이 문제를 해결할 수 있습니다.

    자신 만의 AggregationExpression을 제공하여이 문제를 해결할 수 있습니다.

    ProjectionOperation agg = Aggregation.project() //
    
          .and(new AggregationExpression() {
    
            @Override
            public DBObject toDbObject(AggregationOperationContext context) {
    
              DBObject filterExpression = new BasicDBObject();
              filterExpression.put("input", "$answer_list");
              filterExpression.put("as", "answer");
              filterExpression.put("cond", new BasicDBObject("$eq2", Arrays.<Object> asList("$$answer.question", 2)));
    
              return new BasicDBObject("$filter", filterExpression);
            }
          }).as("profile");
    
  2. from https://stackoverflow.com/questions/39459898/does-spring-data-mongodb-support-filter-array-aggregations-operator by cc-by-sa and MIT license