Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

group by dates in mongodb

I am working on a project in which I am tracking number of clicks on a topic.

I am using mongodb and I have to group number of click by date( i want to group data for 15 days).

I am having data store in following format in mongodb

{     "_id" : ObjectId("4d663451d1e7242c4b68e000"),    "date" : "Mon Dec 27 2010 18:51:22 GMT+0000 (UTC)",    "topic" : "abc",    "time" : "18:51:22" } {      "_id" : ObjectId("4d6634514cb5cb2c4b69e000"),      "date" : "Mon Dec 27 2010 18:51:23 GMT+0000 (UTC)",      "topic" : "bce",      "time" : "18:51:23" } 

i want to group number of clicks on topic:abc by days(for 15 days)..i know how to group that but how can I group by date which are stored in my database

I am looking for result in following format

[   {     "date" : "date in log",     "click" : 9    },     {     "date" : "date in log",     "click" : 19   },   ] 

I have written code but it will work only if date are in string (code is here http://pastebin.com/2wm1n1ix) ...please guide me how do I group it

like image 773
Mark Gill Avatar asked Mar 02 '11 14:03

Mark Gill


People also ask

Can we use group by in MongoDB?

MongoDB group by is used to group data from the collection, we can achieve group by clause using aggregate function and group method in MongoDB. While using aggregate function with group by clause query operations is faster as normal query, basically aggregate function is used in multiple condition.

How do I group multiple fields in MongoDB?

One of the most efficient ways of grouping the various fields present inside the documents of MongoDB is by using the $group operator, which helps in performing multiple other aggregation functions as well on the grouped data.

How do I get group count in MongoDB?

MongoDB group by count multiple fields In MongoDB for counting the multiple fields, we use the aggregation() function. Here, $count is used to count the fields. Example: In this example, we stored some documents in the student collection and use the find() method to check how many documents are in our collection.

What is _ID in Group MongoDB?

The _id expression specifies the group key. If you specify an _id value of null, or any other constant value, the $group stage returns a single document that aggregates values across all of the input documents.


1 Answers

New answer using Mongo aggregation framework

After this question was asked and answered, 10gen released Mongodb version 2.2 with an aggregation framework, which is now the better way to do this sort of query. This query is a little challenging because you want to group by date and the values stored are timestamps, so you have to do something to convert the timestamps to dates that match. For the purposes of example I will just write a query that gets the right counts.

db.col.aggregate(    { $group: { _id: { $dayOfYear: "$date"},                click: { $sum: 1 } } }    ) 

This will return something like:

[     {         "_id" : 144,         "click" : 165     },     {         "_id" : 275,         "click" : 12     } ] 

You need to use $match to limit the query to the date range you are interested in and $project to rename _id to date. How you convert the day of year back to a date is left as an exercise for the reader. :-)

10gen has a handy SQL to Mongo Aggregation conversion chart worth bookmarking. There is also a specific article on date aggregation operators.

Getting a little fancier, you can use:

db.col.aggregate([   { $group: {       _id: {         $add: [          { $dayOfYear: "$date"},           { $multiply:             [400, {$year: "$date"}]          }       ]},          click: { $sum: 1 },       first: {$min: "$date"}     }   },   { $sort: {_id: -1} },   { $limit: 15 },   { $project: { date: "$first", click: 1, _id: 0} } ]) 

which will get you the latest 15 days and return some datetime within each day in the date field. For example:

[     {         "click" : 431,         "date" : ISODate("2013-05-11T02:33:45.526Z")     },     {         "click" : 702,         "date" : ISODate("2013-05-08T02:11:00.503Z")     },             ...     {         "click" : 814,         "date" : ISODate("2013-04-25T00:41:45.046Z")     } ] 
like image 97
Old Pro Avatar answered Sep 23 '22 07:09

Old Pro