MongoDB java group / count函数

时间:2013-02-10 20:31:23

标签: java mongodb

我是MongoDB的新手,我必须使用jsp / servlet创建简单的站点。

我需要一个创建查询,该查询将返回某个网站被访问过的次数。

我的数据库看起来像这样:

{ "_id" : { "$oid" : "5117fa92f1d3a4093d0d3902"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:50.051Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}<br>
{ "_id" : { "$oid" : "5117fa92f1d3a4093d0d3903"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:50.796Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/peta"}<br>
{ "_id" : { "$oid" : "5117fa93f1d3a4093d0d3904"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:51.141Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/peta.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}<br>
{ "_id" : { "$oid" : "5117fa93f1d3a4093d0d3905"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:51.908Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/cetvrta"}<br>
{ "_id" : { "$oid" : "5117fa94f1d3a4093d0d3906"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:52.035Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/cetvrta"}<br>
{ "_id" : { "$oid" : "5117fa94f1d3a4093d0d3907"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:52.197Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/cetvrta.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}

我需要的是一个看起来像这样的结果:

page: localhost:8080/mongoProjekat/treca visited: n(times)<br>
page: localhost:8080/mongoProjekat/druga visited: n(times)

...等等每个访问过的网页。

顺便说一句,我正在使用Java。

2 个答案:

答案 0 :(得分:9)

您可能会发现这个SQL对MongoDB图表http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/有帮助。

关于你当前的问题:

    // getCollection

    DBCollection myColl = db.getCollection("toskebre");

    // for the $group operator
    // note - the collection still has the field name "dolaznaStr"
    // but, to we access "dolaznaStr" in the aggregation command, 
    // we add a $ sign in the BasicDBObject 

    DBObject groupFields = new BasicDBObject( "_id", "$dolaznaStr");

    // we use the $sum operator to increment the "count" 
    // for each unique dolaznaStr 
    groupFields.put("count", new BasicDBObject( "$sum", 1));
    DBObject group = new BasicDBObject("$group", groupFields );


    // You can add a sort to order by count descending

    DBObject sortFields = new BasicDBObject("count", -1);
    DBObject sort = new BasicDBObject("$sort", sortFields );


    AggregationOutput output = myColl.aggregate(group, sort);

    System.out.println( output.getCommandResult() );

println将打印:

{ "serverUsed" : "localhost/127.0.0.1:27017" , 
  "result" : [ { "_id" : "localhost:8080/mongoProjekat/treca" , "count" : 3} , 
               { "_id" : "localhost:8080/mongoProjekat/cetvrta" , "count" : 2} ,
               { "_id" : "localhost:8080/mongoProjekat/peta" , "count" : 1}] , 
  "ok" : 1.0}

答案 1 :(得分:0)

Ty回答凯。我已经找到了类似于你的解决方案。 我这样做了:

     DBObject match = new BasicDBObject("stranica","$dolaznaStr");
     DBObject project = new BasicDBObject("$project",match);

     DBObject id = new BasicDBObject("_id",new BasicDBObject("stranica","$stranica"));
     id.put("posete", new BasicDBObject("$sum", 1));
     DBObject group = new BasicDBObject("$group",id);

     DBObject srt = new BasicDBObject("posete",-1);
     DBObject sort = new BasicDBObject("$sort", srt);
     AggregationOutput ao = collection.aggregate(project, group, sort);