【问题标题】:Aggregate query not working:Arguments must be aggregate pipeline operators聚合查询不起作用:参数必须是聚合管道运算符
【发布时间】:2018-04-22 12:57:09
【问题描述】:

我有如下聚合查询,

ResearchPapers.aggregate([
  {
    $match: params
 },
 {
$group: {
  _id: "$pmid"
},
"Title": {
  "$first": "$Title"
},
"url": {
  "$first": "$url"
},
"year": {
  "$first": "$year"
},
"month": {
  "$first": "$month"
},
"day": {
  "$first": "$day"
}
},
 {
$sort: {
  year: -1,
  month: -1,
  date: -1
  }  }]).exec(function(cerr, records){
    if(cerr){
     return console.log(cerr);
   }
 });

当我执行我的查询时,我得到的错误是`

   Error: Arguments must be aggregate pipeline operators

我找不到 sol,我是 node js 和 mongoose 的新手。谁能给我建议帮助。谢谢。

【问题讨论】:

  • 你能分享一下你到底想做什么。

标签: node.js express mongoose mean-stack


【解决方案1】:

您的大括号不正确,并且您要提前终止 $group 对象。您的第三个排序谓词也引用了一个不存在的字段。这是更正后的管道:

ResearchPapers
.aggregate([
  {
    $match: params
  },
  {
    $group: {
      _id: "$pmid",
      "Title": {
        "$first": "$Title"
      },
      "url": {
        "$first": "$url"
      },
      "year": {
        "$first": "$year"
      },
      "month": {
        "$first": "$month"
      },
      "day": {
        "$first": "$day"
      }
    }
  },
  {
    $sort: {
      year: -1,
      month: -1,
      day: -1
    }
  }
]).exec(function(cerr, records) {
  if (cerr) {
    return console.log(cerr);
  }
});

【讨论】:

    猜你喜欢
    • 2018-04-24
    • 2019-03-27
    • 2014-12-11
    • 2021-06-16
    • 2016-09-17
    • 2017-08-24
    • 1970-01-01
    • 2020-09-10
    • 2021-06-12
    相关资源
    最近更新 更多