如何在java中使用mongodb $ group?

时间:2017-04-26 23:54:31

标签: java json mongodb group-by mongodb-java

我在MongoDB中有一个集合processedClickLog。

{
        "_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中执行复杂的查询。我的查询是获取processedClickLog集合

  1. TAB不等于People-Tab
  2. DOCRANK不等于0
  3. 仅返回“USERID”,“DOCID”,“DOCRANK”,“QUERYTEXT”字段
  4. 按USERID分组
  5. 以下是我的Java代码。我能够满足前三个条件。但是我被困在第四个条件,即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”?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:3)

您必须使用聚合框架。静态导入辅助类的所有方法并使用下面的代码。

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

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();

投影阶段是可选的,只是添加了一个完整的例子。

有关此处聚合的更多信息https://docs.mongodb.com/manual/reference/operator/aggregation/