Mongoose query nested documents greater or less a certain date

前端 未结 1 825
时光取名叫无心
时光取名叫无心 2021-02-14 03:14

How dow I query comments greater than or less than a certain date...

Here is my Schema with Post model and a Comment model:

var mongoose = require(\'mong         


        
1条回答
  •  野的像风
    2021-02-14 03:53

    Use dot notation to reach inside the embedded array docs. For example, to query for the Post comments with a created_at between date1 and date2:

    Post.find({ "comments.created_at": { $gt: date1, $lt: date2 }}, function (err, docs) {
         ...
    });
    

    UPDATE

    Thanks for the edit; now I understand that you're trying to to filter the comments of single post by their created_at date. You can't do that directly with MongoDB queries, but I believe you can do it with the 2.2 aggregation framework if you're at that version. Take a look at the discussion of this feature request on Jira for examples.

    0 讨论(0)
提交回复
热议问题