{"action":"opened","issue":{"url":"https://api.github.com/repos/DataBrewery/cubes-mongo/issues/2","repository_url":"https://api.github.com/repos/DataBrewery/cubes-mongo","labels_url":"https://api.github.com/repos/DataBrewery/cubes-mongo/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/DataBrewery/cubes-mongo/issues/2/comments","events_url":"https://api.github.com/repos/DataBrewery/cubes-mongo/issues/2/events","html_url":"https://github.com/DataBrewery/cubes-mongo/issues/2","id":158388478,"number":2,"title":"mongodb backend uses wrong pipeline order, sums aggregate field name instead of measure (physical) field name","user":{"login":"Stiivi","id":71105,"avatar_url":"https://avatars.githubusercontent.com/u/71105?v=3","gravatar_id":"","url":"https://api.github.com/users/Stiivi","html_url":"https://github.com/Stiivi","followers_url":"https://api.github.com/users/Stiivi/followers","following_url":"https://api.github.com/users/Stiivi/following{/other_user}","gists_url":"https://api.github.com/users/Stiivi/gists{/gist_id}","starred_url":"https://api.github.com/users/Stiivi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Stiivi/subscriptions","organizations_url":"https://api.github.com/users/Stiivi/orgs","repos_url":"https://api.github.com/users/Stiivi/repos","events_url":"https://api.github.com/users/Stiivi/events{/privacy}","received_events_url":"https://api.github.com/users/Stiivi/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-06-03T15:06:31Z","updated_at":"2016-06-03T15:06:31Z","closed_at":null,"body":"Moved from Databrewery/cubes#336 by @miraculixx\r\n\r\n---\r\n\r\nBased on the [tutorial](http://cubes.readthedocs.org/en/v1.0.1/tutorial.html) I converted the tutorial dataset to MongoDB, using the cube with the mongo backend. After [fixing the mongo backend](https://github.com/DataBrewery/cubes/issues/257#issuecomment-72470333) it almost works, except that the MongoBrowser creates a pipeline that returns wrong results:\r\n\r\n```\r\nfrom cubes import Workspace\r\nworkspace = Workspace(config=\"/home/patrick/projects/cubestest/slicer-mongo.ini\")\r\nresult = browser.aggregate(drilldown=[\"year\"])\r\nfor record in result:\r\n    print record\r\n=>\r\n{u'record_count': 31, u'amount_sum': 0, u'year': 2009}\r\n{u'record_count': 31, u'amount_sum': 0, u'year': 2010}\r\n```\r\n\r\nAs you can see the amount_sum fields are 0, which is wrong. Looking at the pipeline generated reveals why:\r\n\r\n```\r\n[{\"$match\": {\"year\": {\"$exists\": 1}}}, \r\n {\"$project\":        <<< wrong order, project pipeline goes after group\r\n        {\"record_count\": \"$record_count\", \r\n        \"amount_sum\": \"$amount_sum\", \"year\": 1}\r\n }, \r\n {\"$group\": {\"record_count\": {\"$sum\": 1}, \r\n         \"_id\": {\"year\": {\"$year\": \"$year\"}}, \r\n         \"amount_sum\": {\"$sum\": \"$amount_sum\"}}},    <<< should be \"$sum:\" : \"$amount\"\r\n  {\"$sort\": {\"_id.year\": 1}}\r\n]\r\n```\r\n\r\nwhen the two problems are fixed manually and the aggregation directly run against mongodb the results are correct:\r\n\r\n```\r\ndb.ibrd.aggregate([{ \"$match\": { \"year\": { \"$exists\": 1 } } }, \r\n{ \"$group\": { \"record_count\": { \"$sum\": 1 }, \"_id\": { \"year\": { \"$year\": \"$year\" } }, \"amount\": { \"$sum\": \"$amount\"} } }, \r\n{ \"$project\": { \"record_count\": \"$record_count\", \"amount\": \"$amount\", \"year\": 1 } }, \r\n{ \"$sort\": { \"_id.year\": 1 } }])\r\n=>\r\n/* 1 */\r\n{\r\n\t\"_id\" : {\r\n\t\t\"year\" : 2009\r\n\t},\r\n\t\"record_count\" : 31,\r\n\t\"amount\" : 550840\r\n},\r\n/* 2 */\r\n{\r\n\t\"_id\" : {\r\n\t\t\"year\" : 2010\r\n\t},\r\n\t\"record_count\" : 31,\r\n\t\"amount\" : 566020\r\n}\r\n```\r\n\r\nThe problem probably occurs within the mapping code. Any hints?\r\n\r\nData ready to import into mongodb: https://gist.github.com/miraculixx/5ffc89241478e8b09883\r\nMongoDB: 3.2\r\ncubes: release-1.0.1 with fix for mongodb applied as per above\r\n\r\n```\r\n#slices.ini\r\n[store]\r\ntype: mongo\r\nurl: mongodb://localhost:27017/\r\ndatabase: cubes\r\ncollection: ibrd\r\n[models]\r\nmain: tutorial_model.json\r\n```\r\n"}}
{"actor":{"display_login":"Stiivi"}}