MongoDB:$size的参数必须是数组,但类型为:missing

MongoDB:$size的参数必须是数组,但类型为:missing,mongodb,aggregation-framework,mongorepository,Mongodb,Aggregation Framework,Mongorepository,它引发错误:org.springframework.dao.InvalidDataAccessApiUsageException:命令执行失败:错误[$size的参数必须是数组,但类型为:missing] mongo文档,我必须从中找到特定调查、问题和选项ID的用户ID计数 { "_id" : ObjectId("5ea31dce0e4d4b09e4db0cd6"), "_class" : "com.litmus7.river.commons.common.model.surve

它引发错误:org.springframework.dao.InvalidDataAccessApiUsageException:命令执行失败:错误[$size的参数必须是数组,但类型为:missing]

mongo文档,我必须从中找到特定调查、问题和选项ID的用户ID计数

{
    "_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;
    }
public int getResultCount(){
聚合=新聚合(
匹配(标准,其中(“调查id”)为(“5EA30DF40E4D4B4D4D7B6D7”),
放松(“调查问题”),
匹配(标准,其中(“调查问题id”)为(“5ea30df40e4d4b4de4d7b6d2”),
放松(“调查问题。问题选项”),
匹配(标准。其中(“调查问题选项选项id”)为(“5ea30df40e4d4b4de4d7b6d1”),
项目()
和(“调查\问题.问题\选项.用户\ id”)
.size()
.作为(“计数”);
AggregationResults=mongoTemplate.aggregate(聚合,“结果”,Object.class);
if(results!=null&&results.getMappedResults()!=null&&results.getMappedResults().size()>0){
整数intCount=(整数)((Map)results.getMappedResults().get(0)).get(“count”);
返回整数计数;
}
返回0;
}
您需要使用操作员:

.project().and(ArrayOperators.arrayOf(ConditionalOperators
    .ifNull("survey_question.question_options.user_id").then(Collections.emptyList())).length())
.as("count");
您需要使用运算符:

.project().and(ArrayOperators.arrayOf(ConditionalOperators
    .ifNull("survey_question.question_options.user_id").then(Collections.emptyList())).length())
.as("count");

这就是你想要做的::()如果是,那么如果它对给定的文档有效!!您需要检查是否有多个文档用于
调查\u id:5ea30df40e4d4b4de4d7b6d7
,然后检查任何文档
“调查问题.问题选项.用户id”
可能缺失!!另外,如果您想获得唯一的
用户id
列表,那么这不是方法。这就是您试图做的::()如果是,那么如果它对给定的文档有效!!您需要检查是否有多个文档用于
调查\u id:5ea30df40e4d4b4de4d7b6d7
,然后检查任何文档
“调查问题.问题选项.用户id”
可能缺失!!此外,如果您想获得唯一的
用户id
列表,那么这不是一种方法。。