uniStatUserSessionLogs.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. async group(data) {
  12. let {
  13. whereJson
  14. } = data;
  15. const dbRes = await this.aggregate(dbName.uniStatUserSessionLogs, {
  16. match: whereJson,
  17. group: {
  18. _id: {
  19. appid: '$appid',
  20. version: '$version',
  21. platform: '$platform',
  22. channel: '$channel',
  23. },
  24. // 用户数(去重复)
  25. user_count: {
  26. $addToSet: '$uid'
  27. },
  28. create_time: {
  29. $min: '$create_time'
  30. }
  31. },
  32. addFields: {
  33. user_count: {
  34. $size: '$user_count'
  35. }
  36. },
  37. // 按创建时间排序
  38. sort: {
  39. create_time: 1
  40. },
  41. getAll: true
  42. });
  43. let list = dbRes.data;
  44. return list;
  45. }
  46. async groupCount(whereJson) {
  47. const dbRes = await this.aggregate(dbName.uniStatUserSessionLogs, {
  48. match: whereJson,
  49. group: {
  50. _id: null,
  51. // 设备数(去重复)
  52. count: {
  53. $addToSet: '$uid'
  54. },
  55. },
  56. addFields: {
  57. count: {
  58. $size: '$count'
  59. }
  60. },
  61. getAll: true
  62. });
  63. try {
  64. return dbRes.data[0].count;
  65. } catch (err) {
  66. return 0;
  67. }
  68. }
  69. }
  70. module.exports = new Dao();