mongodbtemplate 关联表查询报错
我有两个表:url和userClickUrl表,字段如下
url:
String id;
String uid;
String title;
userClickUrl:
String id;
// 跟url中的id关联
String urlId;
String uid;
int clickCount;
现在我想使用userClickUrl为主表,url为子表,根据clickCount排序,查询url数据,但是报错,下面是java代码:
LookupOperation lookupOperation = Aggregation.lookup("url", "urlId", "_id", "url_as");
AggregationOperation match = Aggregation.match(Criteria.where("uid").is(uid));
ProjectionOperation project = Aggregation.project(UserClickUrl.class);
SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "clickCount");
LimitOperation limit = Aggregation.limit(10);
operationList.add(lookupOperation);
operationList.add(match);
operationList.add(addFieldsOperation);
operationList.add(project);
operationList.add(sort);
operationList.add(limit);
Aggregation agg = Aggregation.newAggregation(operationList);
AggregationResults<UserClickUrl> aggregationResults = mongoTemplate.aggregate(agg, "userClickUrl", UserClickUrl.class);
List<UserClickUrl> dataList = aggregationResults.getMappedResults();
2021-09-22 09:50:26.182 ERROR 2119 --- [nio-8902-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Invalid reference 'clickCount'!] with root cause
java.lang.IllegalArgumentException: Invalid reference 'clickCount'!
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:114) ~[spring-data-mongodb-3.1.3.jar:3.1.3]
at org.springframework.data.mongodb.core.aggregation.ExposedFieldsAggregationOperationContext.getReference(ExposedFieldsAggregationOperationContext.java:86) ~[spring-data-mongodb-3.1.3.jar:3.1.3]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我把上面的代码,换成下面的就好了
换成:
是不是就是说Aggregation.project不能使用.class表示,只能通过字符串表示字段field