为什么过滤时找不到文档?

时间:2019-09-21 00:47:59

标签: javascript node.js mongodb mongoose

我正在尝试根据日期获取馆藏。

收藏

[
  {
    "_id": ObjectId("5d8570d0f0b406165142b90a"),
    "Emp No": "A105EUJ2",
    "Emp Name": "CHINKI GUPTA",
    "Card No": "29712",
    "Department": "WIPRO",
    "Gender": "",
    "Att Date": ISODate("2019-09-11T18:30:00Z"),
    "In Time": ISODate("2019-09-12T04:11:00Z"),
    "Out Time": ISODate("2019-09-12T14:26:00Z"),
    "Status": "P",
    "Late By": "",
    "Early By": "",
    "Total Hour": "10:15",
    "OT Hour": "01:15",
    "Location": "",
    "id": "A105EUJ2-12-Sep-2019",
    "__v": 0
  },
  {
    "_id": ObjectId("5d8570d0f0b406165142b90b"),
    "Emp No": "A106NDI7",
    "Emp Name": "CHANDER SEKHAR SEKHAR",
    "Card No": "35323",
    "Department": "IBM",
    "Gender": "",
    "Att Date": ISODate("2019-09-11T18:30:00Z"),
    "In Time": ISODate("2019-09-12T05:55:00Z"),
    "Out Time": ISODate("2019-09-12T12:40:00Z"),
    "Status": "P",
    "Late By": "",
    "Early By": "",
    "Total Hour": "06:45",
    "OT Hour": "00:00",
    "Location": "",
    "id": "A106NDI7-12-Sep-2019",
    "__v": 0
  },
  {
    "_id": ObjectId("5d8570d0f0b406165142b90c"),
    "Emp No": "A10CIDZX",
    "Emp Name": "SANDEEP KUMAR SINGH",
    "Card No": "41629",
    "Department": "ACCENTURE",
    "Gender": "",
    "Att Date": ISODate("2019-09-11T18:30:00Z"),
    "In Time": ISODate("2019-09-12T04:49:00Z"),
    "Out Time": ISODate("2019-09-12T13:46:00Z"),
    "Status": "P",
    "Late By": "",
    "Early By": "",
    "Total Hour": "08:57",
    "OT Hour": "00:00",
    "Location": "",
    "id": "A10CIDZX-12-Sep-2019",
    "__v": 0
  }
]

https://mongoplayground.net/p/PGji97i5xGj

db.collection.aggregate([
  {
    $match: {
      "Att Date": {
        $gte: "2019-09-01",
        $lte: "2019-09-30"
      }
    }
  }
])

预期输出

所有文档都应提取。

1 个答案:

答案 0 :(得分:1)

您要比较"2019-09-01",的字符串和ISODate("2019-09-11T18:30:00Z")的日期

尝试比较日期和日期

db.collection.aggregate([
  {
    $match: {
      "Att Date": {
        $gte: ISODate("2019-09-01"),
        $lte: ISODate("2019-09-30")
      }
    }
  }
])