fixGroupAgg.js 721 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. module.exports = function(schema, options) {
  3. // 为全局mongodb添加一个解决 结果集 在分片模式下 大于101 Cursor no found 的问题
  4. schema.statics.aggregateFix = async function(agg) {
  5. agg.push({
  6. $group: { _id: null, data: { $push: '$$ROOT' } },
  7. });
  8. const result = await this.aggregate(agg).allowDiskUse(true);
  9. if (result && result.length > 0) {
  10. return result[0].data;
  11. }
  12. return [];
  13. };
  14. // 添加一个无条件分组的简化操作
  15. schema.statics.aggregateNGroup = async function(agg) {
  16. const result = await this.aggregate(agg).allowDiskUse(true);
  17. if (result && result.length > 0) {
  18. return result[0];
  19. }
  20. return {};
  21. };
  22. };