如何在Java中使用mongodb $ group? [英] How to use mongodb $group in java?

查看:478
本文介绍了如何在Java中使用mongodb $ group?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中处理了一个collectionClickClickLog.

I have a collection processedClickLog in MongoDB.

{
        "_id" : ObjectId("58ffb4cefbe21fa7896e2d73"),
        "ID" : "81a5d7f48e5df09c9bc006e7cc89d6e6",
        "USERID" : "206337611536",
        "DATETIME" : "Fri Mar 31 17:29:34 -0400 2017",
        "QUERYTEXT" : "Tom",
        "DOCID" : "www.demo.com",
        "TITLE" : "Harry Potter",
        "TAB" : "People-Tab",
        "TOTALRESULTS" : "1",
        "DOCRANK" : 1
}
{      "id":
        ....
}

我正在尝试在Java中执行复杂的查询.我的查询是要获取加工的ClickLog集合,其中

I am trying to execute a complex query in java. My query is to get processedClickLog collection where

  1. TAB不等于People-Tab
  2. DOCRANK不等于0
  3. 仅返回"USERID","DOCID","DOCRANK","QUERYTEXT"字段
  4. 按USERID分组

下面是我的Java代码.我能够满足前三个条件.但是我处于按USERID分组的第4个条件.

Below is my Java code. I am able to satisfy the first three condition. But I am stuck on 4th condition which is group by USERID.

String jsonResult = "";
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("test1");
        MongoCollection<Document> collection = database.getCollection("processedClickLog");
        //add condition where TAB is not equal to "People-Tab" and DOCRANK is not equal to 0
        List<DBObject> criteria = new ArrayList<DBObject>();
        criteria.add(new BasicDBObject("DOCRANK", new BasicDBObject("$ne", 0)));
        criteria.add(new BasicDBObject("TAB", new BasicDBObject("$ne", "People-Tab")));

            //combine the above two conditions
            BasicDBObject query = new BasicDBObject("$and", criteria);
            //to retrieve all the documents with specific fields 
            MongoCursor<Document> cursor = collection.find(query)
                    .projection(Projections.include("USERID", "DOCID", "DOCRANK", "QUERYTEXT")).iterator();
      try {
                while (cursor.hasNext()) {
                    System.out.println(cursor.next().toJson());

                }
            } finally {
                cursor.close();
            }
            System.out.println(hashMap);
            mongoClient.close();
    }

我应该如何定义整个查询以在Java中添加条件"group by USERID"?感谢您的帮助

How should I define my whole query to add the condition "group by USERID" in java? Any help is appreciated

推荐答案

您必须使用聚合框架.静态导入帮助程序类的所有方法,并使用以下代码.

You've to use aggregation framework. Statically import all the methods of helper classes and use the below code.

在较新的3.x驱动程序api中不需要使用BasicDBObject.您应该使用新的类Document满足类似的需求.

Use of BasicDBObject is not required in newer 3.x driver api. You should use the new class Document for similar needs.

import static com.mongodb.client.model.Accumulators.*;
import static com.mongodb.client.model.Aggregates.*;
import static java.util.Arrays.asList;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;

Bson match = match(and(ne("DOCRANK", 0), ne("TAB", "People-Tab")));
Bson group = group("$USERID", first("USERID", "$USERID"), first("DOCID", "$DOCID"), first("DOCRANK", "$DOCRANK"), first("QUERYTEXT", "$QUERYTEXT"));
Bson projection = project(fields(include("USERID", "DOCID", "DOCRANK", "QUERYTEXT"), excludeId()));
MongoCursor<Document> cursor = collection.aggregate(asList(match, group, projection)).iterator();

投影阶段是可选的,仅用于提供完整示例.

Projection stage is optional, only added to give a complete example.

有关聚合的更多信息,请参见 https://docs.mongodb.com/manual/参考/操作员/汇总/

More about aggregation here https://docs.mongodb.com/manual/reference/operator/aggregation/

这篇关于如何在Java中使用mongodb $ group?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆