uniStatUserSessionLogs.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * 数据库操作
  3. */
  4. const BaseMod = require('../../base');
  5. const dbName = require("./config");
  6. class Dao extends BaseMod {
  7. constructor() {
  8. super();
  9. this.tablePrefix = false; // 不使用表前缀
  10. }
  11. // 分组查询
  12. async group(data) {
  13. let {
  14. whereJson
  15. } = data;
  16. const dbRes = await this.aggregate(dbName.uniStatUserSessionLogs, {
  17. match: whereJson, // 匹配查询条件
  18. group: {
  19. _id: {
  20. appid: '$appid',
  21. version: '$version',
  22. platform: '$platform',
  23. channel: '$channel',
  24. },
  25. // 用户数(去重复)
  26. user_count: {
  27. $addToSet: '$uid'
  28. },
  29. create_time: {
  30. $min: '$create_time'
  31. }
  32. },
  33. addFields: {
  34. user_count: {
  35. $size: '$user_count'
  36. }
  37. },
  38. // 按创建时间排序
  39. sort: {
  40. create_time: 1
  41. },
  42. getAll: true
  43. });
  44. let list = dbRes.data;
  45. return list;
  46. }
  47. // 分组统计数量
  48. async groupCount(whereJson) {
  49. const dbRes = await this.aggregate(dbName.uniStatUserSessionLogs, {
  50. match: whereJson, // 匹配查询条件
  51. group: {
  52. _id: null,
  53. // 用户数(去重复)
  54. count: {
  55. $addToSet: '$uid'
  56. },
  57. },
  58. addFields: {
  59. count: {
  60. $size: '$count'
  61. }
  62. },
  63. getAll: true
  64. });
  65. try {
  66. return dbRes.data[0].count;
  67. } catch (err) {
  68. return 0;
  69. }
  70. }
  71. }
  72. module.exports = new Dao();