views:

183

answers:

0

I am trying to execute this MongoDB command in Node.js:

db.events.group({
  key : {
    'timestamp.d' : true
  },
  cond: { 'event_name': 'search' },
  initial : {
    'count' : 0,
    'empty' : 0,
    'redos' : 0
  },
  reduce : function(item, summaries){
    summaries.count++;
    if (item.result_count == 0){
      summaries.empty++;
    }
    if (item.original_query) {
      summaries.redos++;
    }
    var totalSuccesses = (summaries.count - summaries.redos - summaries.empty);
    summaries.percentNonFailures = (totalSuccesses / summaries.count) * 100
  }
})

This works great from the Mongo command like giving me summaries by day. When I try this in Node.js:

db.collection('events', function(err, collection){
  collection.group(
    { "timestamp.d": true },
    { "event_name": "search" },
    {
      count: 0, 
      empty: 0, 
      redos: 0,
      percentNonFailure: 0, 
    }, 
    function(item, summaries){
      summaries.count++;
      if (item.result_count == 0) { summaries.empty++; }
      if (item.original_query) { summaries.redos++; }
      totalSuccesses = summaries.count - summaries.redos - summaries.empty
      summaries.percentNonFailure = (totalSuccesses / summaries.count) * 100
    },
    function(err, results){ self.eventEmitter.emit(doneEvent, results) }
  );
})

I get a single result with all the totals per day summed up together, so basically a sum of count, empty, redos for the entire period.

What is being done wrong in trying to convert the query to use in Node.js?