收藏
回答

云开发数据库如何实现这个效果?

在一张表里有数据:

{"_id":"14139e126137625f0a8b0a4c3302e3f1","totalCount":2,"sid":1007,"mobile":"11111111111","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a4d0bfdd883","totalCount":1,"sid":1007,"mobile":"22222222222","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a4e4d80ff30","totalCount":4,"sid":1007,"mobile":"33333333333","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a4f134e77c9","totalCount":7,"sid":1007,"mobile":"44444444444","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a5074d7bbd1","totalCount":9,"sid":1004,"mobile":"55555555555","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a510e0f2cc5","totalCount":3,"sid":1004,"mobile":"66666666666","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a521c515d4f","totalCount":9,"sid":1003,"mobile":"77777777777","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a532ae56f69","totalCount":4,"sid":1003,"mobile":"88888888888","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}
{"_id":"14139e126137625f0a8b0a542f40ea28","totalCount":2,"sid":1003,"mobile":"99999999999","createdDayStr":"2021-9-7","calcStart":"2021-08-01 00:00:00","calcEnd":"2021-08-31 23:59:59"}

{"_id":"14139e126137625f0a8b0a4c3302e3f1","totalCount":2,"sid":1007,"mobile":"11111111111","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a4d0bfdd883","totalCount":1,"sid":1007,"mobile":"22222222222","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a4e4d80ff30","totalCount":3,"sid":1007,"mobile":"33333333333","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a4f134e77c9","totalCount":9,"sid":1007,"mobile":"44444444444","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a5074d7bbd1","totalCount":4,"sid":1004,"mobile":"55555555555","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a510e0f2cc5","totalCount":6,"sid":1004,"mobile":"66666666666","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a521c515d4f","totalCount":1,"sid":1003,"mobile":"77777777777","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a532ae56f69","totalCount":4,"sid":1003,"mobile":"88888888888","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}
{"_id":"14139e126137625f0a8b0a542f40ea28","totalCount":6,"sid":1003,"mobile":"99999999999","createdDayStr":"2021-9-7","calcStart":"2021-09-01 00:00:00","calcEnd":"2021-09-30 23:59:59"}



需要求出:

calcStart~calcEnd 在 2021-8-1 00:00:00~2021-8-31 23:59:59 和 2021-9-1 00:00:00~2021-9-30 23:59:59 两个自然月里 totalCount >= 4 按 sid 分组后的手机号出现的次数,怎么写?折腾半天,脑子有点晕。。

{"sid":"1007","count":1, "mobileSum":1},// 同一手机号满足 sid=1007 在 8月和9月 totalCount=1,记录数有 1 个
{"sid":"1007","count":2, "mobileSum":2},// 同一手机号满足 sid=1007 在 8月和9月 totalCount=2,记录数有 2 个
{"sid":"1007","count":3, "mobileSum":4},// 同一手机号满足 sid=1007 在 8月和9月 totalCount=3,记录数有 4 个
.
.
.
{"sid":"1007","count":10, "mobileSum":9},// 同一手机号满足 sid=1007 在 8月和9月 totalCount=10,记录数有 9 个
{"sid":"1004","count":1, "mobileSum":0},// 同一手机号满足 sid=1004 在 8月和9月 totalCount=1,记录数有 0 个
{"sid":"1004","count":2, "mobileSum":10},// 同一手机号满足 sid=1004 在 8月和9月 totalCount=2,记录数有 10 个
.
.
.




回答关注问题邀请回答
收藏

1 个回答

  • 易
    2021-09-10

    用聚合,aggregate()

    2021-09-10
    有用
    回复
登录 后发表内容