使用MongoDB和NodeJS的“参数必须是聚合管道运算符”

I have below code which is working fine and displaying 100 records descending order of Vote value from MongoDB collection;

var myAggregate = Entry.aggregate([{ $group: { _id: "$url",author: { $first: "$author" }, vote: { $max: "$vote" }, url: { $first: "$url" }, title: { $first: "$title" } } },{ $sort:{ vote:-1} },{ $limit: 100 }]);

// Home Route
app.get('/', function(req, res) {
  myAggregate.exec(function(err, entries) {
      if (err) {
          console.log(err)
      } else {
          res.render('index', {
              entries: entries
          });
      }
  });
});

我想要实现的是根据今天的日期来过滤此数据。

我的架构如下所示;

{
    "_id": {
        "$oid": "5ec6efe565915b59f10787b4"
    },
    "title": "my title",
    "url": "https://myurl.com",
    "author": "john",
    "created": {
        "$date": "2020-05-22T00:17:14.340Z"
    },
    "vote": 51
}

My index page should display 100 records which are created today.

因此,例如,如果我的条目是在今天的日期和时间范围内创建的,则应显示它。

I tried to follow explanation on this question but did not work for me.I am receiving Arguments must be aggregate pipeline operators error. Any idea what i am missing here?

var start = new Date();
start.setHours(0,0,0,0);

var end = new Date();
end.setHours(23,59,59,999); 

var myAggregate = Entry.aggregate([{ $group: { _id: "$url",author: { $first: "$author" }, vote: { $max: "$vote" }, url: { $first: "$url" }, title: { $first: "$title" } } },{ $sort:{ vote:-1} },{created: {$gte: start, $lt: end}},{ $limit: 100 }]);
评论
  • 俄对花儿笑
    俄对花儿笑 回复

    created is neither an operator in aggregation pipeline nor an operator in MongoDB, I guess.

    I assume that you want to filter documents that fall with the start and end range you can replace this

    {created: {$gte: start, $lt: end}}
    

    {
      $match: {
        $expr: {
          $and: [
            {
              $gte: [
                "$created",
                start
              ]
            },
            {
              $lt: [
                "$created",
                end
              ]
            }
          ]
        }
      }
    }