聚合查询不起作用:参数必须是聚合管道运算符

时间:2017-11-09 13:42:21

标签: node.js express mongoose mean-stack

我有一个针对aggrigate的查询,如下所示,

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,我是节点js和mongoose的新手。任何人都可以建议我帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

您的大括号不正确,并且您要提前终止$ 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);
  }
});