Mongodb 使用特定日期筛选数据-mern

Mongodb 使用特定日期筛选数据-mern,mongodb,express,mongoose,mern,Mongodb,Express,Mongoose,Mern,这里我有日期(即更新日期)和“2020-10-30T09:20:35.086Z”。如何过滤具有特定日期的数据(例如:2020-10-28) 来自服务器的数据 [ { "_id": "5f9bdace778082303c859248", "createdAt": "2020-10-28T09:20:14.335Z", "updatedAt":

这里我有日期(即更新日期)和“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"
      }
    }
  }
])

工作

使用聚合的一种方法

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