mysql case when sum,MongoDB-MySQL SUM(CASE WHEN)等效?

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值