javascript - Aggregation working in console but not node? -
i totally new node.js (and javascript too), idea of anonymous functions, callbacks , likes totally foreign me , i'm still wrapping head around all... said please go easy on me! :)
i've got following query i'm able use in mongo console without issues:
db.warmod_events.aggregate({ $match: { $and: [ { "match.id": 1 }, { "match.event.player.name": 'xxx' }, { "match.event.event": 'round_stats' } ] } }, { $group: { _id : null, kills : { $sum: "$match.kills" } } });
from reading mongo node driver documentation (which seems little brief) came with, reason never returns anything, query result empty , i'm not sure why?
var getkills = function (db, callback) { var collection = db.collection('warmod_events'); collection.aggregate( [ { $match: { and: [ {"match.matchid": 1}, {"match.event.player.name": 'jim'}, {"match.event.event": 'round_stats'} ] } }, { $group: { "_id": null, kills: {$sum: "$match.kills"} } } ], function (err, result) { console.dir(result); callback(result); }); };
an example of document in collection:
{ "match": { "event": { "timestamp": "2015-06-03 15:02:22", "event": "round_stats", "round": 1, "player": { "name": "jim", "userid": 45, "uniqueid": "bot", "team": 2 }, "shots": 0, "hits": 0, "kills": 0, "headshots": 0, "tks": 0, "damage": 0, "assists": 0, "assists_tk": 0, "deaths": 0, "head": 0, "chest": 0, "stomach": 0, "leftarm": 0, "rightarm": 0, "leftleg": 0, "rightleg": 0, "generic": 0 }, "matchid": 1 } }
the and
in $match
should $and
instead:
$match: { $and: [ {"match.matchid": 1}, {"match.event.player.name": 'jim'}, {"match.event.event": 'round_stats'} ] }
however, because $and
terms use unique keys, don't need use $and
, can simplify to:
$match: { "match.matchid": 1, "match.event.player.name": 'jim', "match.event.event": 'round_stats' }
Comments
Post a Comment