错误:参数必须是Aggregate.append上的聚合管道运算符

时间:2020-01-22 00:35:45

标签: node.js mongodb mongoose

这是我的应用程序提供的堆栈跟踪:

Error: Arguments must be aggregate pipeline operators
at Aggregate.append (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:89:11)
at new Aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:48:17)
at Function.aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/model.js:2415:17)
at Function.storeSchema.statics.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/models/Store.js:82:15)
at exports.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/controllers/storeController.js:198:30)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/handlers/errorHandlers.js:11:12
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:335:12)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:174:3)
at router (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:47:12)

相关代码-这是在整个应用程序中调用的仅有的两个地方:

Store.js

storeSchema.statics.getTagsList = function() {
  return this.aggregate([
    { $unwind: '$tags' },
    { $group: { _id: '$tags', count : { $sum: 1 } }},
    { $sort: { count: -1} }
  ]);
};

storeSchema.statics.getTopStores = function (){
  return this.aggregate([
      // Look up stores and populate reviews
    { $lookup: { from: 'reviews', localField: '_id',
      foreignField: 'store', as: 'reviews'}},
      // Filter for stores with 2 or more reviews
    { $match: { 'reviews.1': { $exists: true } }},
      // Add average reviews field
      // TODO: Changed with $addfield in mongodb 3.4?
      // UPDATE
    { $project: {
        photo: '$$ROOT.photo',
        name: '$$ROOT.name',
        reviews: '$$ROOT.reviews',
        slug: '$$ROOT.slug',
        averageRating: { $avg: '$reviews.rating' }
      } },
    // sort it by our new field, highest reviews first
    { $sort: { averageRating: -1 }},
    // limit to at most 10
    { $limit: 10 }
  ])
};

到目前为止,我一直试图找到解决该问题的方法,这使我想到了一些不同或不清楚的示例,或者是未能解决错误消息的文档。当然发生了什么,在代码块的这两个函数中应该使用一些聚合管道运算符,但我不知道该怎么办。

如果愿意,您可以在https://github.com/airbr/whatsgood处深入资料库。

1 个答案:

答案 0 :(得分:0)

解决方案是将Mongoose升级到版本5.0。

此外,我不得不在此答案中添加网址解析器提及内容:

Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true