简体   繁体   English

猫鼬没有返回正确数量的结果

[英]Mongoose not returning the correct number of results

I'm new to mongoose.我是猫鼬的新手。 I'm trying to query by createdAt date, with startDate and endDate, however I got the incorrect number of results.我试图通过createdAt 日期、startDate 和endDate 进行查询,但是我得到的结果数量不正确。

data

{"_id":{"$oid":"5f4fab9beceaa20f898feafb"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Alex","email":"alex@gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T14:26:35.237Z"},"updatedAt":{"$date":"2020-09-02T14:26:35.237Z"}}
{"_id":{"$oid":"5f4fc3677e7b1e2d806714cf"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Joshua","email":"joshua@gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T16:08:07.123Z"},"updatedAt":{"$date":"2020-09-02T16:08:07.123Z"}}
{"_id":{"$oid":"5f50b80f28ca26065b2ac9a5"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Harold","email":"harold@gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-03T09:31:59.112Z"},"updatedAt":{"$date":"2020-09-03T09:31:59.112Z"}}
{"_id":{"$oid":"5f59104ff518c40579b578d0"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Katy","email":"katy@gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-09T17:26:39.787Z"},"updatedAt":{"$date":"2020-09-09T17:26:39.787Z"}}

I have 4 records with the ff.我有 4 条记录与 ff。 date 2020-09-02 , 2020-09-03 and 2020-09-09日期2020-09-02 , 2020-09-032020-09-09

I wanted to get all records from 2020-09-02 and 2020-09-03 , with these I expected 3 results as I have to records on the 2020-09-02 , however I only got 2 results, those records have 2020-09-02 date with them.我想获得2020-09-022020-09-03所有记录,这些记录我预计有 3 个结果,因为我必须在2020-09-02上记录,但是我只得到了 2 个结果,这些记录有2020-09-02与他们约会。

const { limit = 30 } = params;

    return new Promise((resolve, reject) => {
        const query = {
          createdAt: {
            $gte: '2020-09-02',
            $lte: '2020-09-03',
          }
        };
      this.model.find(query).sort({
        createdAt: 'descending',
      }).limit(limit).exec((err, res) => {
        if (!err) {
          resolve(res);
        }
        reject(err);
      })
    })

Did I miss something with my code?我的代码遗漏了什么吗?

I also tried passing new Date('2020-09-02') but I still got same results.我也尝试通过new Date('2020-09-02')但我仍然得到相同的结果。

I tried setting mongoose debug to true and below is what I got.我尝试将 mongoose debug 设置为 true,下面是我得到的。

Mongoose: inquiries.find({ createdAt: { '$gte': new Date("Wed, 02 
Sep 2020 00:00:00 GMT"), '$lte': new Date("Thu, 03 Sep 2020 00:00:00 GMT") }}, { sort: { createdAt: -1 }, limit: 30, projection: {} 
})

Thanks in advance.提前致谢。

Youre looking for records greater than 2020-09-02 00:00:00 and less than 2020-09-03 00:00:00.您正在寻找大于 2020-09-02 00:00:00 且小于 2020-09-03 00:00:00 的记录。

You only have 2 records which are between these values, if you want records including those at 2020-09-03 23:59:59, set your lte to 2020-09-04您只有 2 个记录在这些值之间,如果您想要包括 2020-09-03 23:59:59 的记录,请将您的 lte 设置为 2020-09-04

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM