首页 > 解决方案 > 错误:参数必须是 Aggregate.append 处的聚合管道运算符

问题描述

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

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深入了解存储库。

标签: node.jsmongodbmongoose

解决方案


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

此外,我必须在这个答案中添加 url 解析器提及:

通过将 useNewUrlParser 设置为 true 来避免“不推荐使用当前 URL 字符串解析器”警告


推荐阅读