java - 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" }

我不明白这里的光标选项是什么意思。应该在哪里配置这个选项?

编辑这是一个示例用户文档

{
  "_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 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

Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursorBatchSize(100).build());

使用默认批量大小

Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursor(new Document()).build());

关于java - Spring - mongodb - 聚合 - 需要 'cursor' 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490380/

相关文章:

javascript - 如何在 Node 议程中以编程方式定义多个具有相同名称的作业

node.js - 学习 NodeJS 和 MongoDB Docker 组合

javascript - 如何将文件名设置为与数据库中的对象 ID 相同?

linux - 从 bash 提示符和脚本向 docker exec 传递参数

java - Spring Data Mongodb 的性能问题

c# - MongoDB 身份验证错误

c# - 如何将 MongoDB 驱动程序与 C# ASP.NET Core API 框架一起使用?

node.js - Mongoose 复合索引创建字段顺序

node.js - mongoose.connection.collections.collecti

mongodb - 如何为 mongodb 副本集配置 grails 3