Reputation: 443
Executing the following aggregation pipeline:
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();
}
throws the following exception:
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?
EDIT Here is a sample user document
{
"_id": "5a6df13552f42a34dcca9aa6",
"username": "user1",
"password": "$2a$10$p0OXq5PPa41j1e4iPcGZHuWjoKJ983sieS/ovFI.cVX5Whwj21WYi",
"favoriteItems": [
{
"_id": "5a0c6b2dfd3eb67969316d6d",
"name": "item1",
"city": "Rabat"
},
{
"_id": "5a0c680afd3eb67969316d0b",
"name": "item2",
"city": "Rabat"
}
]
}
Upvotes: 6
Views: 13851
Reputation: 191
You can provide outputmode as cursor as providing a cursor is mandatory
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()));
}
Upvotes: 1
Reputation: 113
'The 'cursor' option is required, except for aggregate with the explain argument'
This type of error raised in spring data when you are using incompatible versions of MongoDB and Spring-data-mongo.
Though you can get rawResults with explain, cursor arguments.
Aggregation aggregation = Aggregation.newAggregation(group).withOptions( new AggregationOptions(allowDiskUse, explain, cursor));
//try with .withOptions( new AggregationOptions(true,false,new Document()));
Passing by commented Arguments you will get result in rawResult but it will not be mapped in given outType.class.
To get mapped result you have to download right dependency of spring-data version according to your MongoDb version.
EDIT
I have used Spring version 5.0.3 and Spring-data-mongoDB version 2.0.3 It is working Fine.
Upvotes: 3
Reputation: 75934
From the docs.
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: }.
You can pass batchSize
with AggregationOptions
in Spring Mongo 2.x version
Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursorBatchSize(100).build());
With default batch size
Aggregation aggregation = newAggregation(unwind, group).withOptions(newAggregationOptions().cursor(new Document()).build());
Upvotes: 4