【问题标题】:Check if document is between two dates using Mongoose and MomentJS使用 Mongoose 和 MomentJS 检查文档是否在两个日期之间
【发布时间】:2020-04-24 12:50:25
【问题描述】:

我查看了类似的帖子,但解决方案尚未奏效,我不知道我做错了什么。我试过用很多不同的方式格式化日期,甚至只是使用 new Date() 库。我尝试使用.toISOString() 转换日期。

这是我试图查询的集合中的一个示例文档。

[
  {
    "_id": ObjectId("5df58d45244a850d54b922c8"),
    "lessons": [
      {
        "_id": ObjectId("5db221be211d7b68ac8be618"),
        "mentorData": {
          "objective": "Ensuring students that making mistakes is normal and that it is a part of life",
          "task": "Post a video explaining obstacles that you had to overcome as a programmer",
          "dueDate": "2019-12-14T15:26:10.000+0000"
        },
        "studentData": {
          "objective": "Learning that failures help you grow",
          "task": "Post a video explaining obstacles that you have overcame",
          "dueDate": "2020-01-14T22:26:10.000+0000" <---- CHECKING THIS DATE
        },
        "title": "How to overcome obstacles",
        "comments": []
      }
    ]    
  }
]

我查询数据库如下:

const query = {
  "lessons.studentData.dueDate": {
    $gte: moment()
      .startOf("isoweek")
      .toDate(),
    $lte: moment()
      .endOf("isoweek")
      .toDate()
  }
};

Class.find(query).then(doc => {
  res.json(doc);
});

返回示例文档,它不应该因为...

lessons.studentData.dueDate的唯一文档是2020-01-14T22:26:10.000+0000 不属于以下之间 $gte 2020-01-06T06:00:00.000Z 和 $lte 2020-01-13T05:59:59.999Z

[
  {
    "_id": ObjectId("5df58d45244a850d54b922c8"),
    "lessons": [
      {
        "_id": ObjectId("5db221be211d7b68ac8be618"),
        "comments": [],
        "mentorData": {
          "dueDate": "2019-12-14T15:26:10.000+0000",
          "objective": "Ensuring students that making mistakes is normal and that it is a part of life",
          "task": "Post a video explaining obstacles that you had to overcome as a programmer"
        },
        "studentData": {
          "dueDate": "2020-01-14T22:26:10.000+0000",
          "objective": "Learning that failures help you grow",
          "task": "Post a video explaining obstacles that you have overcame"
        },
        "title": "How to overcome obstacles"
      }
    ]
  }
]

【问题讨论】:

    标签: mongodb mongoose momentjs


    【解决方案1】:

    从您的代码看来,您正在尝试获取当前一周的文档:

    let startDate = moment().startOf("isoweek").format('YYYY-MM-DD'); //2020-01-06
    let endDate = moment().endOf("isoweek").format('YYYY-MM-DD'); //2020-01-12
    
    Class.aggregate([
        { $unwind: '$lessons' },
        { $addFields: { 'date': { $dateToString: { format: "%Y-%m-%d", date: "$lessons.studentData.dueDate" } } } },
        { $match: { $and: [{ 'date': { $gte: startDate } }, { 'date': { $lte: endDate } }] } },
        { $group: { _id: '$_id', lessons: { $push: '$lessons' } } }
    ])
    

    注意:假设你的lessons.studentData.dueDateISODate(),那么这两个输入日期很难转换成ISODate()格式,所以我们需要把lessons.studentData.dueDate和输入日期带到相同的格式并比较它们,因为dueDate 在一个数组中,我们在比较之前展开和转换。

    参考: $dateToString

    收集数据:

    /* 1 */
    {
        "_id" : ObjectId("5df58d45244a850d54b922c8"),
        "lessons" : [ 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be618"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-06T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-01T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-15T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }
        ]
    }
    
    /* 2 */
    {
        "_id" : ObjectId("5df58d45244a850d54b922d8"),
        "lessons" : [ 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be618"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-16T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-18T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-15T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }
        ]
    }
    
    /* 3 */
    {
        "_id" : ObjectId("5df58d45244a850d54b923c8"),
        "lessons" : [ 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be618"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                }
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                }
            }, 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be628"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                }
            }
        ]
    }
    
    /* 4 */
    {
        "_id" : ObjectId("5e1403ba627ef78236d1505d"),
        "lessons" : []
    }
    

    结果:

    /* 1 */
    {
        "_id" : ObjectId("5df58d45244a850d54b922c8"),
        "lessons" : [ 
            {
                "_id" : ObjectId("5db221be211d7b68ac8be618"),
                "mentorData" : {
                    "objective" : "Ensuring students that making mistakes is normal and that it is a part of life",
                    "task" : "Post a video explaing obstacles that you had to overcome as a programmer",
                    "dueDate" : ISODate("2019-12-14T15:26:10.000Z")
                },
                "studentData" : {
                    "objective" : "Learning that failures help you grow",
                    "task" : "Post a video explaining obstacles that you have overcame",
                    "dueDate" : ISODate("2020-01-06T22:26:10.000Z")
                },
                "title" : "How to overcome obstacles",
                "comments" : []
            }
        ]
    }
    

    【讨论】:

    • 这行得通,奇怪的是我必须为此使用聚合。
    • @Snoopy :我必须在日期上工作,从代码中获取 ISODate() 几个星期有点棘手(你可以轻松进入 ISO 字符串),因为你的 dueDate 不是顶级字段便于操作,所以我发现.aggregation() 很有帮助或唯一的方法,也许如果你可以从你的 js 代码中将startDateendDate 转换为与dueDate 相同的格式,那么你可能不需要转换 dueDate 然后可以使用 .find() 来完成。
    • 感谢您为我节省了一些时间,伙计。我认为它不是顶级密钥可能会导致问题。
    猜你喜欢
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2013-04-11
    • 2012-09-19
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多