Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mongodb 3 java:密钥的聚合和_Java_Mongodb_Aggregation Framework_Mongodb Java - Fatal编程技术网

mongodb 3 java:密钥的聚合和

mongodb 3 java:密钥的聚合和,java,mongodb,aggregation-framework,mongodb-java,Java,Mongodb,Aggregation Framework,Mongodb Java,我有一个收藏,它有_id和searchKey。我想知道每个存储密钥的计数,比如“java 2,C++ 3”。 我的实体是 public class SearchKeyModel implements Serializable { private static final long serialVersionUID = 2099119595418807689L; private String searchKey; private Integer count; 如下

我有一个收藏,它有_id和searchKey。我想知道每个存储密钥的计数,比如“java 2,C++ 3”。 我的实体是

public class SearchKeyModel implements Serializable {

    private static final long serialVersionUID = 2099119595418807689L;

    private String searchKey;
    private Integer count;  
如下所示:

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());
Iterator sortedList=集合
.aggregate(Arrays.asList(新文档($match),新文档(“searchKey”,1)),
新文档(“$sort”,新文档(“count”,-1)),
新文档(“$group”,新文档(“\u id”,null)。追加(“计数”,新文档(“$sum”,1)
))
)).iterator();
System.out.println(“list hasNext”+sortedList.hasNext());
while(sortedList.hasNext()){
Document doc=sortedList.next();
SearchKeyModel实体=gson.fromJson(doc.toJson(),SearchKeyModel.class);
列表。添加(实体);
}
System.out.println(“列表大小为”+list.size());
但是sortedList.hasNext()始终为false


有人能帮助您理解如何完成此操作吗?

您可以尝试下面的聚合以获得所需的结果

添加了
$project
stage,以获得与Gson的java类字段名称相同的键名的输出,从而正确映射它们

我还将查询更改为使用静态助手方法

List<Bson> aggregation = Arrays.asList(
                Aggregates.group("$searchKey", Accumulators.sum("count", 1)),
                Aggregates.sort(Sorts.descending("count")),
                Aggregates.project(Projections.fields(Projections.excludeId(), Projections.computed("searchKey", "$_id"), Projections.include("count"))));

 Iterator<Document> sortedList = collection.aggregate(aggregation).iterator();
List aggregation=Arrays.asList(
聚合.group($searchKey),累加器.sum(“count”,1)),
聚合.排序(排序.降序(“计数”),
聚合.project(Projections.fields(Projections.excludeId()、Projections.computed(“searchKey”)、“$\u id”)、projects.include(“count”));
Iterator sortedList=collection.aggregate(aggregation.Iterator();

您可以尝试下面的聚合以获得所需的结果

添加了
$project
stage,以获得与Gson的java类字段名称相同的键名的输出,从而正确映射它们

我还将查询更改为使用静态助手方法

List<Bson> aggregation = Arrays.asList(
                Aggregates.group("$searchKey", Accumulators.sum("count", 1)),
                Aggregates.sort(Sorts.descending("count")),
                Aggregates.project(Projections.fields(Projections.excludeId(), Projections.computed("searchKey", "$_id"), Projections.include("count"))));

 Iterator<Document> sortedList = collection.aggregate(aggregation).iterator();
List aggregation=Arrays.asList(
聚合.group($searchKey),累加器.sum(“count”,1)),
聚合.排序(排序.降序(“计数”),
聚合.project(Projections.fields(Projections.excludeId()、Projections.computed(“searchKey”)、“$\u id”)、projects.include(“count”));
Iterator sortedList=collection.aggregate(aggregation.Iterator();