【问题标题】:mongodb date range filter error when document have array documents embebed当文档嵌入数组文档时,mongodb日期范围过滤器错误
【发布时间】:2022-01-04 17:32:30
【问题描述】:

我在过滤一个日期时遇到了一个问题,该日期包含在 MongoDB 中嵌入的数组中。

文档 1:

{
    "_id": {
        "$oid": "61d406681512ba17533b35c9"
    },
    "items": [{
        "_id": {
            "$oid": "61d406681512ba17533b35cd"
        },
        "bookingEnd": {
            "$date": "2022-04-22T00:00:00.000Z"
        },
        "bookingStart": {
            "$date": "2022-03-19T00:00:00.000Z"
        }
    }, {
        "_id": {
            "$oid": "61d406681512ba17533b35cd"
        },
        "bookingEnd": {
            "$date": "2022-07-22T00:00:00.000Z"
        },
        "bookingStart": {
            "$date": "2022-06-19T00:00:00.000Z"
        }
    }],
    "createdAt": {
        "$date": "2022-01-04T08:33:44.958Z"
    },
    "updatedAt": {
        "$date": "2022-01-04T08:33:44.958Z"
    },
    "__v": 0
}

文档 2:

{
    "_id": {
        "$oid": "61d407c41512ba17533b35f7"
    },
    "items": [{
        "_id": {
            "$oid": "61d407c41512ba17533b35fb"
        }
        "bookingEnd": {
            "$date": "2022-05-22T00:00:00.000Z"
        },
        "bookingStart": {
            "$date": "2022-04-19T00:00:00.000Z"
        }
    }],
    "createdAt": {
        "$date": "2022-01-04T08:39:32.648Z"
    },
    "updatedAt": {
        "$date": "2022-01-04T08:39:32.648Z"
    },
    "__v": 0
}

简历:

我有 2 个文件:

  1. 文档 1(嵌入数组中的 2 个元素):
  • items.0.bookingStart: "2022-03-19T00:00:00.000Z"
  • items.1.bookingStart: "2022-06-19T00:00:00.000Z"
  1. 文档 1(嵌入数组中的 1 个元素):
  • items.0.bookingStart: "2022-04-19T00:00:00.000Z"

Mongo 过滤器:

{ 'items.bookingStart': { $gte: ISODate('2022-03-01T00:00:00.000Z'), $lte: ISODate('2022-03-30T00:00:00.000Z') } }

猫鼬过滤器:


  async findAll(params: AdminFilterOrderDto) {
    const {
      bookingStartInitial,
      bookingStartFinal,
    } = params;

    const filter = {
      'items.bookingStart': {
        $gte: bookingStartInitial,
        $lte: bookingStartFinal,
      },
    };

    return await this.orderModel.find(filter);
  }

应用过滤器:

  1. $gte: "2022-02-01T00:00:00.000Z"; $lte: "2022-02-30T00:00:00.000Z" (它没有显示任何好的东西)
  2. $gte: "2022-03-01T00:00:00.000Z"; $lte: "2022-03-30T00:00:00.000Z" (显示一个文件。好)
  3. $gte: "2022-04-01T00:00:00.000Z"; $lte: "2022-04-30T00:00:00.000Z" (显示两个文档。不好。应该显示一个文档。
  4. $gte: "2022-05-01T00:00:00.000Z"; $lte: "2022-05-30T00:00:00.000Z" (它显示一个文档。不好。它不应该显示任何东西。
  5. $gte: "2022-06-01T00:00:00.000Z"; $lte: "2022-06-30T00:00:00.000Z" (显示一个文件。好)
  6. $gte: "2022-07-01T00:00:00.000Z"; $lte: "2022-07-30T00:00:00.000Z"(它什么也没显示。好)

过滤器 3 和 4 给出错误的响应。

知道发生了什么吗?是 MongoDB 错误还是我执行查询错误?

感谢您的帮助!

【问题讨论】:

  • 要比较日期字段值,MongoDB 文档字段和提供的输入参数数据类型必须相同。您可以使用$type 运算符来查找文档的字段类型。

标签: mongodb mongoose mongodb-query


【解决方案1】:

使用$elemMatch

db.collection.find({
  items: {
    $elemMatch: {
      $and: [
        {
          "bookingStart": {
            $gte: {
              "$date": "2022-04-01T00:00:00.000Z"
            }
          }
        },
        {
          "bookingStart": {
            $lte: {
              "$date": "2022-04-30T00:00:00.000Z"
            }
          }
        }
      ]
    }
  }
})

mongoplayground

【讨论】:

  • @gil.newe 完美!如果您能接受这个答案并投赞成票,那就太好了。
  • 对不起,我的声望没有 15 :cry:
猜你喜欢
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 2014-04-12
  • 2011-01-09
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多