哪位大神能把这句sql用mongodb写出来啊?

SELECT COUNT(DISTINCT(ccc)) AS ccc,COUNT(DISTINCT(bbb)) AS bbb FROM total WHERE user_id='$user_id' AND ADDDATE BETWEEN '$time[starttime]' AND '$endtime' GROUP BY aaa ORDER BY aaa DESC

sql语句如上,哪位大神指点一下。

阅读 2.4k
2 个回答

考虑使用MongoDB的aggregation:

1、COUNT(DISTINCT(ccc)) AS ccc,COUNT(DISTINCT(bbb)) AS bbb

$group+$sum

2、user_id='$user_id' AND ADDDATE BETWEEN '$time[starttime]' AND '$endtime'

$match

供参考。

Love MongoDB!Have fun!

给你个我做过的例子:

db.runCommand({"distinct":"flyStat","key":"user","query":{"sys":"o2o","page":"index_groupBuy",$and:[{"date":{$gt:ISODate("2015-12-08T00:00:00.213Z")}},{"date":{$lt:ISODate("2015-12-09T00:00:00.213Z")}}]}}).values.length; 

等价于:

select count(distinct user) from flyStat where sys=o2o and page=index_groupBuy and date>=151208 and date<=151209;

order很好写,你搜一下吧,group没写过

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题