It throws an error :org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed: Error [The argument to $size must be an array, but was of type: missing]
The mongo document from which I have to find the count of userid of for specific survey,question,option ids
{
"_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"
]
}
]
}
]
}
** The Aggregation function written : **
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;
}
You need to use $ifNull operator:
.project().and(ArrayOperators.arrayOf(ConditionalOperators
.ifNull("survey_question.question_options.user_id").then(Collections.emptyList())).length())
.as("count");