加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring – mongodb – aggregation – ‘cursor

发布时间:2020-12-15 01:47:01 所属栏目:大数据 来源:网络整理
导读:执行以下聚合管道: public void getMostLikedItems () { UnwindOperation unwind = Aggregation.unwind("favoriteItems"); GroupOperation group = Aggregation.group("favoriteItems").count().as("likes"); SortOperation sort = Aggregation.sort(Sort.D

执行以下聚合管道:

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());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读