我需要使用 mongoDB Paging.count() 方法进行总计数,但添加条件时速度会变慢 有解决方法吗?

发布于 2025-01-12 04:03:29 字数 1491 浏览 2 评论 0原文

mongodb 分页需要总计数。 有没有一种快速方法可以在不使用 PagingModel.count() 方法的情况下获取它?

数据量超过100万条。

--add content

我们需要这个方法的原因是 count 包含一个条件。

exports.getTotalCount = async (shelterName = '', deviceId = '', eventType = '', period = '') => {
  return new Promise((resolve, reject) => {
    let where = {};
    let collectTime = {};
    if (period == 'entire' || period == '') {
      collectTime['collectTime'] = {$lte: new Date(dayjs().format())}
    } else if (period == 'today') {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "d").format()), $lte: new Date(dayjs().format())}
    } else if (period == 'week') {
      collectTime['collectTime'] = {$gte: new Date(dayjs().add(-7, "d").format()), $lte: new Date(dayjs().format())}
    } else if (period == 'month') {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "M").format()), $lte: new Date(dayjs().format())}
    } else {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-3, "M").format()), $lte: new Date(dayjs().format())}
    }

    where = {
      'thingId': {$regex: shelterName},
      'deviceId': {$regex: deviceId},
      'eventState' : {$regex: eventType},
      'collectTime': collectTime['collectTime']
    };

    EventModel
        .find(where)
        .count()
        .lean()
        .then(data => {
          resolve(data);
        }).catch(err => {
      console.log(err);
    })
  });
}

每次添加一个条件,都需要很长时间。有什么办法可以解决这个问题吗?

A total count is required for mongodb paging.
Is there a quick way to get it without using the PagingModel.count() method?

The amount of data is over 1 million.

--add content

The reason we need this method is that count contains a condition.

exports.getTotalCount = async (shelterName = '', deviceId = '', eventType = '', period = '') => {
  return new Promise((resolve, reject) => {
    let where = {};
    let collectTime = {};
    if (period == 'entire' || period == '') {
      collectTime['collectTime'] = {$lte: new Date(dayjs().format())}
    } else if (period == 'today') {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "d").format()), $lte: new Date(dayjs().format())}
    } else if (period == 'week') {
      collectTime['collectTime'] = {$gte: new Date(dayjs().add(-7, "d").format()), $lte: new Date(dayjs().format())}
    } else if (period == 'month') {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-1, "M").format()), $lte: new Date(dayjs().format())}
    } else {
      collectTime['collectTime'] = {$gt: new Date(dayjs().add(-3, "M").format()), $lte: new Date(dayjs().format())}
    }

    where = {
      'thingId': {$regex: shelterName},
      'deviceId': {$regex: deviceId},
      'eventState' : {$regex: eventType},
      'collectTime': collectTime['collectTime']
    };

    EventModel
        .find(where)
        .count()
        .lean()
        .then(data => {
          resolve(data);
        }).catch(err => {
      console.log(err);
    })
  });
}

Every time a condition is added, it takes a really long time. Is there any way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2025-01-19 04:03:29

collection.count() 是最快的,因为它读取集合元数据信息......

collection.count() is the quickest since it reads the collection metadata info ...

森林很绿却致人迷途 2025-01-19 04:03:29

您可以有一个不同的集合,在其中存储集合的统计信息并保持更新(例如,将文档添加到“跟踪”集合,同时将“统计”集合中的计数添加 1)。

You can have a different collection in which you store the statistics for the collection and you keep it updated (e.g. you add a document to the "tracking" collection, you also add 1 to the count in the "statistics" collection).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文