I have collection which is have _id and searchKey . I want to know count of each stored key like "java 2, C++ 3 " . My entity is
public class SearchKeyModel implements Serializable {
private static final long serialVersionUID = 2099119595418807689L;
private String searchKey;
private Integer count;
Aggregation like this:
Iterator<Document> sortedList = collection
.aggregate(Arrays.asList(new Document("$match", new Document("searchKey", 1)),
new Document("$sort", new Document("count", -1)),
new Document("$group", new Document("_id", null).append("count", new Document("$sum", 1)
))
)).iterator();
System.out.println("list hasNext " + sortedList.hasNext());
while (sortedList.hasNext()) {
Document doc = sortedList.next();
SearchKeyModel entity = gson.fromJson(doc.toJson(), SearchKeyModel.class);
list.add(entity);
}
System.out.println("list size is " + list.size());
But sortedList.hasNext() always false.
Can anyone help to understand how to finish this?