I’m using nodejs, mongoose odm, and mongo for a web app and am running into issues trying to run “group by” style query in mongoose:
var results = mymodel.collection.group (
{
keyf:
function(doc)
{
var m = doc.date.getMonth();
var d = doc.date.getDate();
var y = doc.date.getFullYear();
return { date: m + "/" + d + "/" + y };
},
cond: {},
reduce: function(doc,prev) { prev.total += doc.value; },
initial: { total: 0 }
}
);
if(results == null)
{
console.log("results is null\n");
}
if I run the “mymodel.collection.group” code in mongo shell, it works perfectly. However, in nodejs/mongoose it seems to return a null result even though the mongoose documentation states that direct mongo code could be executed against the native mongo driver.
does anyone have any ideas how to resolve?
I have just managed to do it in Mongoose. To do it you need to create a chained call and in the callback do the reduce.
So the above is the map and then reduce is
That works for my needs at the moment