Aggregation framework for MongoDB -


i have following schema:

customer id location name time of visit 

the above stores information of customer's visit @ various locations.

i know if there's way write aggregate query in mongodb, gives total visitor information different sections of day, per day per location.

sections of day:

edit:

12  -  8 8   -  11 11  -  1 pm 1 pm   -  4 pm 4 pm   -  8 pm 8 pm   -  12 pm 

if customer visits location on same day , same section of day more once, should counted once. however, if customer visits location on same day different sections of day, should counted once each of section of day has appeared in.

example:

customer 1 visits store on day 1 @ 9:30 customer 1 visits store on day 1 @ 10:30 pm customer 1 visits store b on day 2 @ 9:30 customer 1 visits store b on day 2 @ 11:30 customer 1 visits store b on day 2 @ 2:45 pm  customer 2 visits store on day 1 @ 9:45 customer 2 visits store b on day 1 @ 11:00 customer 2 visits store b on day 2 @ 9:45 

final output of repeat visits:

store b, day 1, section (00:00 - 08:00) : 0 visitors store b, day 1, section (08:00 - 16:00) : 2 visitors store b, day 1, section (16:00 - 24:00) : 1 visitors store b, day 2, section (00:00 - 08:00) : 0 visitors store b, day 2, section (08:00 - 16:00) : 2 visitors store b, day 2, section (16:00 - 24:00) : 0 visitors 

is there way above kind of query done using aggregation framework mongodb?

yes, can done quite simply. it's similar query describe in answer previous question, rather aggregating day, need aggregate day-hour-combinations.

to start with, rather doing group need project new part of date need transform "time of visit" field appropriate hour form. let's @ 1 way it:

{$project : { newdate: {                   y:{$year:"$tov"}, m:{$month:"$tov"}, d:{$dayofmonth:"$tov"},                    h: { $subtract :                            [ { $hour : "$tov" },                              { $mod : [ { $hour : "$tov" }, 8 ] }                            ]                       }              },              customerid:1, locationid:1             } } 

as can see generates year, month, day , hour hour truncated mod 8 (so 0, 8(am), or 16 aka 4pm.

next can same steps did before, aggregating different level of time granularity.

there other ways of achieving same thing, can see some examples of date manipulation on blog.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -