{"action":"closed","issue":{"url":"https://api.github.com/repos/ozomer/node-red-contrib-mongodb2/issues/4","labels_url":"https://api.github.com/repos/ozomer/node-red-contrib-mongodb2/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/ozomer/node-red-contrib-mongodb2/issues/4/comments","events_url":"https://api.github.com/repos/ozomer/node-red-contrib-mongodb2/issues/4/events","html_url":"https://github.com/ozomer/node-red-contrib-mongodb2/issues/4","id":123008911,"number":4,"title":"aggregate.toArray not working as expected","user":{"login":"brunnels","id":560075,"avatar_url":"https://avatars.githubusercontent.com/u/560075?v=3","gravatar_id":"","url":"https://api.github.com/users/brunnels","html_url":"https://github.com/brunnels","followers_url":"https://api.github.com/users/brunnels/followers","following_url":"https://api.github.com/users/brunnels/following{/other_user}","gists_url":"https://api.github.com/users/brunnels/gists{/gist_id}","starred_url":"https://api.github.com/users/brunnels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brunnels/subscriptions","organizations_url":"https://api.github.com/users/brunnels/orgs","repos_url":"https://api.github.com/users/brunnels/repos","events_url":"https://api.github.com/users/brunnels/events{/privacy}","received_events_url":"https://api.github.com/users/brunnels/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-18T19:12:31Z","updated_at":"2015-12-19T13:10:01Z","closed_at":"2015-12-19T13:10:01Z","body":"I checked npm and I'm using version 0.2.2.\r\n\r\nI am able to get this working using the collection operation on the mongodb2 node and it returns 3 records as expected.\r\n\r\nWhen I try the same aggregate using Dynamic (msg.operation) aggregate.toArray I get output that looks similar to find.toArray and no aggregation looks to be happening.  The payload array objects have the original columns rather than the ones defined in the aggregate group and all records in the collection are returned.\r\n\r\nWorking using collection operation\r\n```js\r\n var coll = msg.payload;\r\n var agr = coll.aggregate([\r\n     { $sort: { date: 1}},\r\n     { $group: { \r\n         _id: '$topic',\r\n         value: { $last: \"$value\"},\r\n         lastDoc: { $last: \"$_id\"}\r\n     } }\r\n ]).toArray(function(err, results) {\r\n     var newMsg = {};\r\n    \r\n     results.forEach(function(result) {\r\n        var time = result.lastDoc.getTimestamp()\r\n        time.setHours(time.getUTCHours() - 6);\r\n        newMsg[result._id] = {\r\n            value: result.value,\r\n            time: time.toLocaleString()\r\n        };\r\n     });\r\n\r\n     node.send(newMsg);\r\n });\r\n```\r\n\r\nNot working Dynamic (msg.operation)\r\n```js\r\nvar newMsg = {};\r\nnewMsg.operation = 'aggregate.toArray';\r\nnewMsg.collection = 'temps';\r\nnewMsg.payload = [\r\n    { $sort: { date: 1}},\r\n    { $group: { \r\n        _id: '$topic',\r\n        value: { $last: \"$value\"}\r\n    } }\r\n];\r\nreturn newMsg;\r\n```\r\n\r\nhandler function\r\n```js\r\nvar newMsg =[];\r\nfor (var i = 0; i < msg.payload.length; i++) {\r\n    var item = {};\r\n    var time = msg.payload[i].lastDoc.getTimestamp()\r\n    time.setHours(time.getUTCHours() - 6);\r\n    item[msg.payload[i]._id] = { \r\n        value: msg.payload[i].value,\r\n        time: time.toLocaleString()\r\n    };\r\n    newMsg.push(item);\r\n};\r\nreturn  {payload: newMsg};\r\n```"}}