Can't compare moment dates in MongoDb Can't compare moment dates in MongoDb mongoose mongoose

Can't compare moment dates in MongoDb


So the problem here is that you have two collections, posts and comments. Based on your Posts schema, comments array contains only ids that reference documents that are stored in second collection. That's why you can check whether that array exists and is not empty but you can't refer directly to these elements.

To fix that you can use $lookup to get those documents from comments into posts and then you can apply your date condition inside $match, try:

let posts = await Post.aggregate([    { $match: { comments: { $exists: true, $ne: [] }, _postedBy: userId } },    { $lookup: { from: "comments", localField: "comments", foreignField: "_id", as: "comments" } },    { $match: { 'comments.created_date': { $gte: todayForEvent } } }])