【问题标题】:Filter the data with specific dates - mern过滤具有特定日期的数据 - mern
【发布时间】:2021-02-13 17:24:22
【问题描述】:

在这里,我的日期(即 updatedAt)为 "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));

【问题讨论】:

    标签: mongodb express mongoose mern


    【解决方案1】:

    一种使用聚合的方式

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

    工作Mongo playground

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多