错误:参数必须是 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)

相关代码 - 这是整个应用程序中唯一调用聚合的两个地方:

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

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

此外,我不得不添加 url 解析器提及,如以下答案:

通过将 useNewUrlParser 设置为 true 来避免"当前 URL 字符串解析器已弃用"警告