Home > OS >  I need total count using mongoDB Paging.count() method, but it gets slow when condition is added Any
I need total count using mongoDB Paging.count() method, but it gets slow when condition is added Any

Time:03-08

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?

CodePudding user response:

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

CodePudding user response:

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).

  • Related