I am trying to fetch the data from the MongoDB to get the top 5 most liked products.
But I'm getting below error. (MongoDB version 3.2.21)
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" }
DBObject groupFields = groupFields = new BasicDBObject("_id", 0);
groupFields.put("_id", "$productId");
groupFields.put("avgRating", new BasicDBObject("$avg", "$productRvR"));
groupFields.put("productModelName", new BasicDBObject("$push", "$productMN"));
DBObject group = new BasicDBObject("$group", groupFields);
DBObject projectFields = new BasicDBObject("_id", 0);
projectFields.put("productId", "$_id");
projectFields.put("avgRating", "$avgRating");
projectFields.put("productModelName", "$productModelName");
DBObject project = new BasicDBObject("$project", projectFields);
DBObject sort = new BasicDBObject();
sort.put("avgRating",-1);
DBObject orderby=new BasicDBObject("$sort",sort);
DBObject limit=new BasicDBObject("$limit",5);
AggregationOutput aggregate userReviews.aggregate(group,project,orderby,limit);
LinkedHashMap<String, String> mostLikedProductsMap = new LinkedHashMap<String, String>();
for (DBObject queryResult : aggregate.results()) {
BasicDBObject obj = (BasicDBObject) queryResult;
BasicDBList productModelName = (BasicDBList) obj.get("productModelName");
mostLikedProductsMap.put((String)productModelName.get(0),obj.getString("avgRating"));
}
return mostLikedProductsMap;