Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB java group/count function

Tags:

java

mongodb

I am new to MongoDB, and I have to create simple site using jsp/servlet.

I need to a create query that will return count of how many times some site has been visited.

My DB looks like this:

{ "_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"}

What I need is a result that will look something like this:

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

...and so on for every page that has been visited.

I am using Java, by the way.

like image 269
toskebre Avatar asked Feb 10 '13 20:02

toskebre


People also ask

Can we use count with aggregate function in MongoDB?

MongoDB $count AggregationThe MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.

How do I count all documents in MongoDB aggregation?

The $count stage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field called passing_scores .

How do I count records in MongoDB?

count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

What is Group function in MongoDB?

$group. The $group stage separates documents into groups according to a "group key". The output is one document for each unique group key. A group key is often a field, or group of fields. The group key can also be the result of an expression.


1 Answers

You may find this SQL to MongoDB chart http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/ helpful.

As for your immediate question:

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

The println will print:

{ "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}
like image 117
Kay Avatar answered Sep 17 '22 21:09

Kay