Skip to content

Instantly share code, notes, and snippets.

@alexshyba
Created November 25, 2015 22:11
Show Gist options
  • Save alexshyba/51cd43df21455cf7205a to your computer and use it in GitHub Desktop.
Save alexshyba/51cd43df21455cf7205a to your computer and use it in GitHub Desktop.
Mongo: accessing goal properties in XDB with map reduce
var map = function m() {
for (var p = 0; p < this.Pages.length; p++)
{
if(this.Pages[p].PageEvents){
for (var e = 0; e < this.Pages[p].PageEvents.length; e++){
var key = this.Pages[p].PageEvents[e].Name;
var text = this.Pages[p].PageEvents[e].Text;
if(text && text.indexOf('=' > -1) && text.indexOf('&' > -1)){
var keys = text.split("&");
if(keys.length > 0)
{
var valueIndex = 1;
if(keys[valueIndex]){
var value = keys[valueIndex].split("=")[1];
if(value > 0)
emit(key, +value);
}
}
}
}
}
}
}
var reduce = function r(id,docs) {
return Array.sum(docs);
}
db.Interactions.mapReduce(map,reduce,{query: { StartDateTime: {
$gte: ISODate("2014-10-01T00:00:00.000Z"),
$lt: ISODate("2015-12-17T00:00:00.000Z")}},
out:"orders"});
db.orders.find()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment