Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchElementException throws while using @Aggregation to group #4546

Closed
human-user opened this issue Nov 3, 2023 · 2 comments
Closed

NoSuchElementException throws while using @Aggregation to group #4546

human-user opened this issue Nov 3, 2023 · 2 comments
Labels
type: task A general task

Comments

@human-user
Copy link

human-user commented Nov 3, 2023

my case :

@Aggregation(value = "[{ \"$group\" : { \"_id\" : \"$taskStatus\", \"count\" : { \"$sum\" : 1}}}]")
AggregationResults<CountScanTaskRespDto> countByTaskStatus();
@Document("scan_task")
public class ScanTask {
    @Id
    private String id;
    private String name;
    private Integer taskStatus;
}

public class CountScanTaskRespDto {
    private Integer count;
}

It work well while using mongoTemplate.aggregate :

AggregationResults<CountScanTaskRespDto> result = mongoTemplate.aggregate(
        aggregation, ScanTask.class, CountScanTaskRespDto.class);
return result.getMappedResults();

exception stack log:

java.util.NoSuchElementException: null
	at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:758) ~[na:na]
	at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:778) ~[na:na]
	at org.springframework.data.mongodb.core.aggregation.AggregationOperation.getOperator(AggregationOperation.java:66) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.aggregation.AggregationPipeline.isOut(AggregationPipeline.java:165) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.aggregation.AggregationPipeline.isOutOrMerge(AggregationPipeline.java:99) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.repository.query.StringBasedAggregation.computeOptions(StringBasedAggregation.java:185) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.repository.query.StringBasedAggregation.doExecute(StringBasedAggregation.java:112) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:118) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136) ~[spring-data-commons-3.1.1.jar:3.1.1]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120) ~[spring-data-commons-3.1.1.jar:3.1.1]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-3.1.1.jar:3.1.1]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) ~[spring-data-commons-3.1.1.jar:3.1.1]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.10.jar:6.0.10]
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:72) ~[spring-data-commons-3.1.1.jar:3.1.1]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.10.jar:6.0.10]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-6.0.10.jar:6.0.10]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.10.jar:6.0.10]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:244) ~[spring-aop-6.0.10.jar:6.0.10]
	at jdk.proxy2/jdk.proxy2.$Proxy126.countByTaskStatus(Unknown Source) ~[na:na]
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 3, 2023
@christophstrobl
Copy link
Member

The @Aggregation accepts a String array containing the individual pipeline stages as outlined below.

@Aggregation("{ '$project': { '_id' : '$lastname' } }")
List<Person> ...

@Aggregation(pipeline = {
        "{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }", 
        "{ '$sort' : ... }
})
List<Person> ...

Please remove the outer square brackets to archive the expected behaviour.

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 3, 2023
@human-user
Copy link
Author

@christophstrobl It worked well after remove the outer square. I have saw the pull request. Could you tell me why not support to parse the json array instead of throw a IllegalStateException?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 6, 2023
@mp911de mp911de closed this as completed in 77699dd Nov 6, 2023
mp911de added a commit that referenced this issue Nov 6, 2023
Consistent offending method name format.

Use String.format(…) instead of formatted(…).

Original pull request: #4547
See #4546
@mp911de mp911de linked a pull request Nov 6, 2023 that will close this issue
@mp911de mp911de added type: task A general task and removed status: feedback-provided Feedback has been provided labels Nov 6, 2023
@mp911de mp911de added this to the 4.1.6 (2023.0.6) milestone Nov 6, 2023
mp911de pushed a commit that referenced this issue Nov 6, 2023
This commit makes sure to fail early if an annotated string based annotation does not contain a syntactically valid pipeline.

Original pull request: #4547
Closes #4546
mp911de added a commit that referenced this issue Nov 6, 2023
Consistent offending method name format.

Use String.format(…) instead of formatted(…).

Original pull request: #4547
See #4546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants