【问题标题】:How to use mongodb $group in java?如何在 java 中使用 mongodb $group?
【发布时间】:2017-04-26 23:54:30
【问题描述】:

我在 MongoDB 中有一个已处理的 ClickLog 集合。

{
        "_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 集合

  1. TAB 不等于 People-Tab
  2. DOCRANK 不等于 0
  3. 只返回“USERID”、“DOCID”、“DOCRANK”、“QUERYTEXT”字段
  4. 按 USERID 分组

以下是我的 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 中添加条件“按 USERID 分组”?任何帮助表示赞赏

【问题讨论】:

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


    【解决方案1】:

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

    在较新的 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/

    【讨论】:

    • 谢谢。我会试试这个,让你知道
    • 工作就像一个魅力。非常感谢
    • 你能简单介绍一下group部分吗?我在参考链接中没有找到与此相关的任何内容。
    • 不客气。它与 sql 中的group 概念相同。您按一个键分组并运行accumulators 来收集数据。有关如何将组与 java 驱动程序一起使用的更多信息,请转到此处mongodb.github.io/mongo-java-driver/3.1/builders/aggregation/…
    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 2021-06-11
    • 2017-02-11
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多