过滤具有特定日期的数据 - mern

Filter the data with specific dates - mern

这里我的日期(即更新时间)为“2020-10-28T09:20:14.335Z”和“2020-10-30T09:20:35.086Z”。如何过滤特定日期的数据(例如:2020-10-28)。

来自服务器的数据

[
    {
        "_id": "5f9bdace778082303c859248",
        "createdAt": "2020-10-28T09:20:14.335Z",
        "updatedAt": "2020-10-28T09:20:14.335Z",
        "__v": 0
    },
    {
        "_id": "5f9bdae3778082303c859249",
        "createdAt": "2020-10-30T09:20:35.086Z",
        "updatedAt": "2020-10-30T09:20:35.086Z",
        "__v": 0
    }
]

路由:它给出空数组,如何让它工作?

Attendance.find({ "updatedAt": { "$gte": "2020-10-28", "$lte": "2020-10-28" } })
.then(data => res.json(data))
.catch(err => res.status(400).json('Error: ' + err));

路线:但这行得通

Attendance.find({ "updatedAt": { "$gte": "2020-10-28T09:20:14.335Z", "$lte": "2020-10-28T09:20:14.335Z" } })
.then(data => res.json(data))
.catch(err => res.status(400).json('Error: ' + err));

您可以使用聚合的一种方式

db.collection.aggregate([
  {
    $addFields: {
      "updatedAt": {
        "$substr": [
          "$updatedAt",
          0,
          10
        ]
      }
    }
  },
  {
    $match: {
      "updatedAt": {
        "$gte": "2020-10-28",
        "$lte": "2020-10-28"
      }
    }
  }
])

工作Mongo playground