I'm trying to do some testing with Mongo, I have figured some of the simpler MySQL queries Mongo equivalent.
I have this query that is a little more complex that I need help with...
SELECT DISTINCT dims_user,
COUNT(DISTINCT asset_name) AS asset_count,
COUNT(DISTINCT system_name) AS station_count,
SUM(CASE WHEN details ='viewed' then 1 Else 0 end) AS viewed_count,
SUM(CASE WHEN details Like 'Viewed Web%' then 1 Else 0 end) AS Web_count,
SUM(CASE WHEN details = 'ThumbView' then 1 Else 0 end) AS ThumbView_count,
SUM(CASE WHEN details Like 'Exported%' then 1 Else 0 end) AS Exported_count,
SUM(CASE WHEN details Like '%Print%' then 1 Else 0 end) AS Printed_count
FROM asset_log GROUP BY dims_user;
解决方案
I would agree with Rudu here as you should try to break the details into keys of a mongo document.
The document might contain an object like this:
details:
{
viewed: true
thumb_view: true
web_viewed: false
exported: true
...
}
If you don't restructure the data, the query will require non-rooted regular expressions which are unable to use the indexing features of MongoDB.
However, regardless of if you decide to do that or not you will want to use map reduce for this. You could emit items that include the details during the map (either by processing them with regular expressions or simply emiting the keys in restructured form) and sum them in the reduce phase.
You can read more about it in the docs