Spring - mongodb - aggregation - 'cursor'选项是必需的

时间:2018-01-28 19:21:06

标签: java spring mongodb spring-boot aggregation-framework

执行以下聚合管道:

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

3 个答案:

答案 0 :(得分:2)

来自文档。

  

MongoDB 3.4不推荐使用不带游标的aggregate命令   选项,除非管道包含说明选项。什么时候   使用aggregate命令返回聚合结果,   使用默认批处理大小光标指定游标选项:{}或   在游标选项光标中指定批处理大小:{batchSize:   }。

您可以在Spring Mongo 2.x版本中使用AggregationOptions传递Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursorBatchSize(100).build());

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

使用默认批量大小

import requests
from bs4 import BeautifulSoup
import pandas as pd
#scraping data
page=requests.get('https://www.sec.gov/Archives/edgar/data/11860/0000011860-00-000025.txt')
soup=BeautifulSoup(page.content,'html.parser')
data_1=list(soup.children)[8]
main_data=list(data_1.children)[1].get_text()

#number of words
num=len(main_data.split())

答案 1 :(得分:1)

'The 'cursor' option is required, except for aggregate with the explain argument'

当您使用MongoDB和Spring-data-mongo的不兼容版本时,Spring数据中会出现此类错误。

虽然你可以使用explain,cursor arguments获取rawResults。

Aggregation aggregation = Aggregation.newAggregation(group).withOptions( new AggregationOptions(allowDiskUse, explain, cursor));

//try with .withOptions( new AggregationOptions(true,false,new Document()));

通过注释参数传递,您将获得rawResult中的结果,但不会在给定的outType.class中进行映射。

要获得映射结果,您必须根据MongoDb版本下载spring-data版本的正确依赖项。

修改

我使用 Spring版本5.0.3 Spring-data-mongoDB版本2.0.3 它运作良好。

答案 2 :(得分:1)

您可以提供outputmode作为光标,因为必须使用光标

List<DBObject> list = new ArrayList<DBObject>();
list.add(unwind.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(group.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(sort.toDBObject(Aggregation.DEFAULT_CONTEXT));

DBCollection col = mongoTemplate.getCollection("users");

Cursor cursor = col.aggregate(list, AggregationOptions.builder().allowDiskUse(true).outputMode(OutputMode.CURSOR).build());

List<AggregationResultVO> result = new ArrayList<AggregationResultVO>();

while(cursor.hasNext()) {
     DBObject object = cursor.next();
     result.add(new AggregationResultVO(object.get("aggregationResultId").toString()));
}