Spring - mongodb - aggregation - The #39;cursor#39; option is required(Spring - mongodb - 聚合 - cursor 选项是必需的)
问题描述
执行以下聚合管道:
public void getMostLikedItems () {
UnwindOperation unwind = Aggregation.unwind("favoriteItems");
GroupOperation group = Aggregation.group("favoriteItems").count().as("likes");
SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "likes");
Aggregation aggregation = newAggregation(unwind, group, sort);
DBObject result = mongoTemplate.aggregate(aggregation, "users", LikedItem.class).getRawResults();
}
抛出以下异常:
com.mongodb.MongoCommandException: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument", "code" : 9, "codeName" : "FailedToParse" }
我不明白这里的光标选项是什么意思.应该在哪里配置这个选项?
I don't understand what is meant by cursor option here. Where should this option be configured?
编辑这是一个示例用户文档
{
"_id": "5a6df13552f42a34dcca9aa6",
"username": "user1",
"password": "$2a$10$p0OXq5PPa41j1e4iPcGZHuWjoKJ983sieS/ovFI.cVX5Whwj21WYi",
"favoriteItems": [
{
"_id": "5a0c6b2dfd3eb67969316d6d",
"name": "item1",
"city": "Rabat"
},
{
"_id": "5a0c680afd3eb67969316d0b",
"name": "item2",
"city": "Rabat"
}
]
}
推荐答案
来自文档.
MongoDB 3.4 不赞成使用不带游标的聚合命令选项,除非管道包含解释选项.什么时候使用聚合命令内联返回聚合结果,使用默认批量大小光标指定光标选项:{} 或在游标选项 cursor: { batchSize:}.
MongoDB 3.4 deprecates the use of aggregate command without the cursor option, unless the pipeline includes the explain option. When returning aggregation results inline using the aggregate command, specify the cursor option using the default batch size cursor: {} or specify the batch size in the cursor option cursor: { batchSize: }.
您可以在 Spring Mongo 2.x 版本中使用 AggregationOptions 传递 batchSize
You can pass batchSize with AggregationOptions in Spring Mongo 2.x version
Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursorBatchSize(100).build());
使用默认批量大小
Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursor(new Document()).build());
这篇关于Spring - mongodb - 聚合 - 'cursor' 选项是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring - mongodb - 聚合 - 'cursor' 选项是必需的
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
