我得到'排序超过内存限制...'错误,它指示在我的聚合中使用allowDiskUse(true)。我的问题是,我无法弄清楚在哪里添加到我的代码。我曾尝试将它作为管道中的对象添加,并作为aggregate()方法调用中的一个属性添加,并在运行时遇到错误。Add AllowDiskUse(true)to aggregation
代码如下:
server.get('/cheap-flight-by-route', function (req, res, next) {
Flights.aggregate(
{$sort: {'fare.total_price': 1}},
{$lookup: {
from: 'travelroutes',
localField: 'route',
foreignField: '_id',
as: 'routes'
}},
{$match: {
'routes._id': {$exists: true}
}},
{$group: {
_id: {
departureAirport: '$routes.departureAirport',
arrivalAirport: '$routes.arrivalAirport',
},
total_price: {$min: '$fare.total_price'},
avg_price: {$avg: '$fare.total_price'},
created: {$first: '$created'},
doc: {$first: '$$ROOT'}
}
},
{$project: {
departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
created : '$created',
price: '$total_price',
averagePrice: '$avg_price',
'doc': 1,
'_id': 0
}},
{$sort: {
'created': 1,
'departureAirport': 1,
'arrivalAirport': 1
},
},
function(err, cheapFlights){
if (err) {
log.error(err)
return next(new errors.InvalidContentError(err.errors.name.message))
}
res.send(cheapFlights)
next()
}
) // <-- if I add a .allowDiskUse(true) here it throws a 'bad property' error
})
谢谢尤里,管道的例子工作时,我补充说: .allowDiskUse(true)之前的.cursor() - 我想你只是忘了补充说。感谢您的帮助! – slippedon