Mongoose.countDocuments在计算去年创建的文档时不起作用

问题描述 投票:0回答:1

我正在开发一个名为wertik-js的开源库,它使用graphql,我添加了报告类型助手,这些助手返回基于创建于的猫鼬模型的计数。在这种情况下,我这里有诸如total_count,total_added_last_year等,total_added_today等的报告,对于今天,我已经通过这种方式进行了过滤:

model.countDocuments(
  {
    created_at: {
      $gt: moment().startOf("day"),
      $lt: moment().endOf("day"),
    },
  },
  function (err, count) {
    resolve(count);
  }
);

而且效果很好。当用于去年时,我有去年创建的数据。去年以这种方式计算在内

model.countDocuments(
  {
    created_at: {
      $gt: moment().subtract(1, "year").startOf("year"),
      $lt: moment().subtract(1, "year").endOf("year"),
    },
  },
  function (err, count) {
    resolve(count);
  }
);

但是去年计数不起作用,并且每次返回0时都可以,但是我确定我写错了查询的方式,并且返回了错误的数字,即0。

要重现此错误,可以拉此分支https://github.com/Uconnect-Technologies/wertik-js/tree/188-graphql-or-rest-api-enpoint-to-show-all-stats和:

  1. 安装软件包并运行服务器sudo yarn && sudo yarn dev
  2. 转到http://localhost:4000/
  3. 并执行graphql查询:
{
  roleStats {
    total_count
    total_added_this_month
    total_added_this_week
    total_added_last_7_days
    total_added_today
    total_added_last_month
    total_added_last_90_days
    total_added_last_year
    total_added_this_year
  }
}

您将能够重现该错误。

您可以从猫鼬第110行开始检查此文件https://github.com/Uconnect-Technologies/wertik-js/blob/188-graphql-or-rest-api-enpoint-to-show-all-stats/src/framework/reporting/index.ts#L110。>>

我在这里做错了什么?

任何帮助都将被视为对该开源项目的贡献。

我正在开发一个名为wertik-js的开源库,它使用graphql,我添加了报告类型助手,这些助手返回基于创建于的猫鼬模型的计数。在这种情况下,我有...

node.js mongodb mongoose graphql apollo
1个回答
0
投票

我认为问题出在这行中

© www.soinside.com 2019 - 2024. All rights reserved.