6

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.

1

2 Answers 2

10

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}
Sign up to request clarification or add additional context in comments.

Comments

0

Ty for answer Kay. I allready found solution that is similar to yours. I have done it this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.