Snoopy
Snoopy

Reputation: 1357

Check if document is between two dates using Mongoose and MomentJS

I have looked over similar posts but the solutions have yet to work, I have no clue what I am doing wrong. I have tried formatting the date in so many different ways and even just using the new Date() library besides moment. I tried converting the dates using .toISOString().

Here is an example document in collection I am attempting to query.

[
  {
    "_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": []
      }
    ]    
  }
]

I am querying the DB as follows:

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

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

Which returns the example document, which it shouldn't because...

lessons.studentData.dueDate of the only document is 2020-01-14T22:26:10.000+0000 Which does NOT fall in between the following $gte 2020-01-06T06:00:00.000Z and $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"
      }
    ]
  }
]

Upvotes: 1

Views: 361

Answers (1)

whoami - fakeFaceTrueSoul
whoami - fakeFaceTrueSoul

Reputation: 17915

From your code it seems to be your trying to fetch documents for the ongoing week :

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' } } }
])

Note : Assuming your lessons.studentData.dueDate in ISODate() then it would be hard to get those two input dates into ISODate() format, So we need to bring lessons.studentData.dueDate & input dates to same format & compare those, As dueDate is in an array we're unwinding and transforming before comparison.

Ref : $dateToString

Collection Data :

/* 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" : []
}

Result :

/* 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" : []
        }
    ]
}

Upvotes: 1

Related Questions