I have a document like this:
order : 1
event : { timestamp: 1/1/2012, employeeName: "mick" },
event : { timestamp: 1/1/2012, employeeName: "mick" },
event : { timestamp: 1/2/2012, employeeName: "rick" },
event : { timestamp: 1/3/2012, employeeName: "mick" }
order : 2
event : { timestamp: 1/2/2012, employeeName: "mick" },
event : { timestamp: 1/2/2012, employeeName: "rick" }
I want to run a map-reduce query to return a list of results grouped by date with a count of employee events per order.
In this case Mick had 2 events on the 1/1 on a single order. All the other days had a single event by employee on each order on the 2 and 3 of november. So i need a MAP function with results that would look like:
{ orderId: 1, date: 1/1/2012, employee: "mick", orderEventsCount: 2 },
{ orderId: 1, date: 1/2/2012, employee: "rick", orderEventsCount: 1 },
{ orderId: 2, date: 1/2/2012, employee: "mick", orderEventsCount: 1 },
{ orderId: 2, date: 1/2/2012, employee: "rick", orderEventsCount: 1 },
{ orderId: 1, date: 1/3/2012, employee: "mick", orderEventsCount: 1 }
Then i need a REDUCE function that will take these results and group by Date only and return a count per day of employees with multiple events on a single order:
{ date: 1/1/2012, multipleEventsPerOrdercount: 1 },
{ date: 1/2/2012, multipleEventsPerOrdercount: 0 },
{ date: 1/3/2012, multipleEventsPerOrdercount: 0 }
Since Mick was the only employee to have multiple events on a single date on a single order the result returned only a count of one employee with multiple events on an order on a date.
What would be the best way to write this map-reduce Raven query using LINQ in .NET?
Thanks
Assuming your classes look like this:
Then the index you are asking for would look like this:
And you would use it like this:
The trick here is that you are making a determination in the Map about how much you want each event to contribute to the count. If there is only one event, you contribute zero. When there are multiple events, each event contributes a fraction of the total. These fractions are later summed up in the reduce, returning you to near whole numbers. The double floating point math should get you back to whole numbers, but you still may want to round to the nearest whole integer in your client side code just to be safe.
This also assumes that all events are in the same timezone and you don’t care about daylight savings changes, or the times are in UTC. If neither, then you should use DateTimeOffset and you have more to consider when deciding what each employee’s concept of a Day is.