1

I'm pretty new with moment.js. I have a notification page and on load I would like to display my posts where someone commented. I only want to see the posts where someone commented today. Can someone tell where I went wrong?

I have the following code:

I want to show all the posts where comments were placed on the local start of the day. Then I want to transform this date to UTC time because the mongoDB comment document got saved in UTC time as well.

// Generate the actual time
    const todayForEvent = moment().startOf('day')
      .utc().format();

    console.log('todayForEvent', todayForEvent);

    const posts = await Post.find({
        // From this user...
        $and: [
          // Find normal posts that has comments (recent interactions)
          { _posted_by: userId },
          { comments: { $exists: true, $ne: [] } },
          { 'comments.created_date': { $gte: todayForEvent } }
        ]

    })
console.log(posts);

this is how I save the date of the comment with Mongoose. What I want to do is save it in UTC time

const CommentSchema = new Schema({

  created_date: {
    type: Date,
    default: moment.utc().format()
  }

});

const Comment = mongoose.model('Comment', CommentSchema);

module.exports = Comment;

I did two console.logs and I'll add a screenshot of my database document as well

console.log(todayForEvent) returns 2019-01-02T06:00:00Z

console.log(posts) returns an empty array, so no matches were found

Database screenshot: as you can see below the format is 2019-01-02 12:26:23.000

enter image description here

tilly
  • 2,229
  • 9
  • 34
  • 64
  • 1
    It looks like you might need to save your dates in a different format, according to this question: https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb – jmcgriz Jan 02 '19 at 19:43
  • You can delete the question as I already answered it here https://stackoverflow.com/questions/54015831/mongoose-mongodb-search-where-i-need-values-of-unpopulated-property] – Ashh Jan 03 '19 at 08:18

0 Answers0