MongoDB: аргумент $ size должен быть массивом, но был типа: отсутствует - PullRequest
1 голос
/ 24 апреля 2020

Выдает ошибку: org.springframework.dao.InvalidDataAccessApiUsageException: сбой выполнения команды: ошибка [Аргумент $ size должен быть массивом, но его тип: отсутствует]

Пн go документ, из которого я должен найти количество идентификаторов пользователей для конкретных c опросов, вопросов, идентификаторов опций

{
    "_id" : ObjectId("5ea31dce0e4d4b09e4db0cd6"),
    "_class" : "com.litmus7.river.commons.common.model.survey.Result",
    "survey_id" : "5ea30df40e4d4b4de4d7b6d7",
    "survey_question" : [ 
        {
            "question_id" : "5ea30df40e4d4b4de4d7b6d2",
            "question_options" : [ 
                {
                    "option_id" : "5ea30df40e4d4b4de4d7b6d1",
                    "user_id" : [ 
                        "111", 
                        "222"
                    ]
                }, 
                {
                    "option_id" : "5ea30df40e4d4b4de4d7b6cf",
                    "user_id" : [ 
                        "333", 
                        "444"
                    ]
                }
            ]
        }, 
        {
            "question_id" : "5ea30df40e4d4b4de4d7b6d6",
            "question_options" : [ 
                {
                    "option_id" : "5ea30df40e4d4b4de4d7b6d3",
                    "user_id" : [ 
                        "111", 
                        "222"
                    ]
                }, 
                {
                    "option_id" : "5ea30df40e4d4b4de4d7b6d5",
                    "user_id" : [ 
                        "333", 
                        "444"
                    ]
                }
            ]
        }
    ]
}

** Написана функция агрегирования: **

public int getResultCount() {
        Aggregation aggregation = newAggregation(
            match(Criteria.where("survey_id").is("5ea30df40e4d4b4de4d7b6d7")),
            unwind("survey_question"),
            match(Criteria.where("survey_question.question_id").is("5ea30df40e4d4b4de4d7b6d2")),
            unwind("survey_question.question_options"),
            match(Criteria.where("survey_question.question_options.option_id").is("5ea30df40e4d4b4de4d7b6d1")),
            project()
                .and("survey_question.question_options.user_id")
                .size()
                .as("count"));

        AggregationResults<Object> results = mongoTemplate.aggregate(aggregation, "result", Object.class);
        if (results != null && results.getMappedResults() != null && results.getMappedResults().size() > 0) {
            Integer intCount = (Integer) ((Map) results.getMappedResults().get(0)).get("count");
            return intCount;
        }
        return 0;
    }

1 Ответ

0 голосов
/ 25 апреля 2020

Вам необходимо использовать $ ifNull оператор:

.project().and(ArrayOperators.arrayOf(ConditionalOperators
    .ifNull("survey_question.question_options.user_id").then(Collections.emptyList())).length())
.as("count");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...